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

生命价值不在于获取多少,生命价值在于付出多少。有钱的人更有钱,这往往是一种必然。

本文实例为大家分享了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抓取网页中链接的静态图片请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战