python3 爬图片
2018-11-27 本文已影响0人
ma8345
#coding:utf8
import urllib.request
import re
import os
import urllib
def getHtml(url):
request = urllib.request.Request(url)
page = urllib.request.urlopen(request)
html = page.read()
return html
def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext'
imgre = re.compile(reg)
imglist = re.findall(imgre,html.decode("utf-8"))
x=1
paths = 'D:\\imgs'
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'{}\\{}.jpg'.format(paths,x))
x += 1
return imglist
html = getHtml("")
getImg(html)