加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python抓取网页中链接的静态图片

发布时间:2020-12-17 07:29:44 所属栏目:Python 来源:网络整理
导读:本文实例为大家分享了python抓取网页中链接的静态图片的具体代码,供大家参考,具体内容如下 # -*- coding:utf-8 -*- #http://tieba.baidu.com/p/2460150866 #抓取图片地址 from bs4 import BeautifulSoup import urllib.request from time import sleep htm

本文实例为大家分享了python抓取网页中链接的静态图片的具体代码,供大家参考,具体内容如下

# -*- coding:utf-8 -*- 
 
#http://tieba.baidu.com/p/2460150866 
#抓取图片地址 
 
from bs4 import BeautifulSoup 
import urllib.request 
from time import sleep 
 
html_doc = "http://tieba.baidu.com/p/2460150866" 
 
def get_image(url): 
 req = urllib.request.Request(url) 
 webpage = urllib.request.urlopen(req) 
 
 html = webpage.read() 
 soup = BeautifulSoup(html,'html.parser') 
 
 #抓取图片地址 
 #抓取img标签且class为BDE_Image的所有内容 
 img_src=soup.findAll("img",{'class':'BDE_Image'}) 
 i = 1 
 for img in img_src: 
  img_url = img.get('src') #抓取src 
 # print(img) 
  req = urllib.request.Request(img_url) 
  u = urllib.request.urlopen(req) 
  data = u.read() 
  with open("AutoCodePng20180119-"+str(i)+".jpg",'wb') as f: 
   sleep(2) 
   f.write(data) 
   i += 1 
 
def getImg(url): 
 html = urllib.request(url) 
 page = html.read() 
 soup = BeautifulSoup(page,"html.parser") 
 imglist = soup.find_all('img') #发现html中带img标签的数据,输出格式为<img xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,存入集合 
 lenth = len(imglist) #计算集合的个数 
 for i in range(lenth): 
  print imglist[i].attrs['src'] #抓取img中属性为src的信息,例如<img src="123456" xxxxxxxxxxxxxxxx,则输出为123456 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • python抓取网页图片示例(python爬虫)
  • python抓取网页中图片并保存到本地
  • python抓取网页图片并放到指定文件夹
  • 简单的抓取淘宝图片的Python爬虫
  • python抓取网页中的图片示例
  • Python使用代理抓取网站图片(多线程)
  • python小技巧之批量抓取美女图片
  • 使用Python3编写抓取网页和只抓网页图片的脚本
  • Python使用正则表达式抓取网页图片的方法示例
  • python抓取豆瓣图片并自动保存示例学习

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读