共计 697 个字符,预计需要花费 2 分钟才能阅读完成。
自定义关键字,down百度图片
#!/usr/bin/env python
#coding:utf-8
import re
import requests
import os
word = raw_input("输入表情关键字:")
savedir = raw_input("请输入保存路径: ")
url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+word+'&ct=201326592&v=flip'
result = requests.get(url)
print ('已找到关键词:'+word+'的图片,开始下载图片...')
html = requests.get(url).text
pic_url = re.findall('"objURL":"(.*?)",',html,re.S)
i = 0
if os.path.isdir(savedir) == False:
os.mkdir(savedir)
for each in pic_url:
print ('正在下载第 '+ str(i + 1) +' 张图片,图片地址: '+ str(each))
try:
pic= requests.get(each, timeout=10)
except requests.exceptions.ConnectionError:
print("错误,图片不存在")
continue
string = '/picture/'+str(i) + '.jpg'
with open(string,'wb') as fp:
fp.write(pic.content)
i += 1
print ('下载完成,保存至'+savedir+'')
正文完