python 图片爬取

2019-04-15 17:55发布

data/attach/1904/pk6ukeh3899stri6qmk2qtmob6s1zz38.jpg import re
import urllib.request
import chardet
from lxml import etree
from bs4 import BeautifulSoup


url = "https://www.qiushibaike.com/pic/"
header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0",
"Referer":"https://www.qiushibaike.com/"}




def fun(a,b,c):
    precent = a*b*1.0/c
    if precent>1:
    precent = 1
    else:
    print("%.2f"%(precent*100)+'%')
    #a:已经下载的模块
    #b:数据块的大小
    #c:远程文件的大小
req = urllib.request.Request(url,data = None,headers = header)
ope = urllib.request.urlopen(req)
html = ope.read().decode("utf-8")
reg = r'src="(.+?.jpeg)"'
img = re.compile(reg)
condition = re.findall(img,html)
for imgurl in condition:
imgurl = 'https:'+imgurl
imgname = imgurl.rsplit('/',1)
urllib.request.urlretrieve(imgurl,imgname[1],fun)