下面程序能够爬取百度图片一个网页里面的所以图片,值得关注的是匹配字段,正则表达式要写正确,虽然匹配成功的但是爬取的图片还是较少,下篇我会采取get方法来请求更多图片来爬取。
import urllib.request
import re
import time
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360EE')
page = urllib.request.urlopen(req)#打开网页
html = page.read().decode('utf-8')#网页解码
return html
def get_img(html):
p = r'"(https:[^"]+\.jpg)'#匹配的字段
imglist = re.findall(p,html)
#print(html)
for each in imglist:
print(each)
filename = each.split("/")[-1]
urllib.request.urlretrieve(each,filename,None)#保存图片
#time.sleep(0.01)
if __name__ == '__main__':
#网址
url = 'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1559194736650_R&pv=&ic=&nc=1&z=&hd=&latest=©right=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&sid=&word=%E5%8A%A8%E6%BC%AB%E5%B0%91%E5%A5%B3'
get_img(open_url(url))