python爬取图片简记

参考:https://blog.csdn.net/tanlangqie/article/details/79506543

# -*- coding:utf-8 -*-
import urllib
import urllib.request
import re

def getHtml(url):
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
html = response.read()
return html

def getImg(html):
reg = 'data-original="(.+?\.jpg)"'
imgre = re.compile(reg)
imglist = re.findall(imgre, html.decode('utf-8'))
localpath='G:/photo/'
x = 1
for imgurl in imglist :
urllib.request.urlretrieve(imgurl,localpath+'%s.jpg' % x)
print('正在下载第%s张图片' % x)
x+=1
if x>20:
break
return None

html = getHtml("https://www.zhihu.com/question/27364360")
getImg(html)

猜你喜欢

转载自www.cnblogs.com/Traveller-Lee/p/8954483.html