制作一个爬虫个别分以下几个步骤:
剖析需要
剖析网页源代码,配合开发者工具
编写正则表达式或者 XPath 表达式
正式编写 python 爬虫代码
代码
import re
import requests
def dowmloadPic(html, keyword,limit):
pic_url = re.findall('"objURL":"(.*?)",', html, re.S)
i = 1
print('已找到关键词:' + keyword + '的图片,当初开始下载图片...')
for each in pic_url:
print('正在下载第' + str(i) + '张图片,图片地址:' + str(each))
try:
pic = requests.get(each, timeout=10)
except requests.exceptions.ConnectionError:
print('【谬误】以后图片无奈下载')
continue
dir = './images/' + keyword + '_' + str(i) + '.jpg'
fp = open(dir, 'wb')
fp.write(pic.content)
fp.close()
i += 1
if i > limit:
break
if name == ‘__main__’:
word = input("请输出搜寻关键词:")
limit = int(input("请输出下载数量:"))
url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&ct=201326592&v=flip'
result = requests.get(url)
dowmloadPic(result.text, word,limit)
后果