资源简介
python代码,可以从百度图片获取给定关键词的所有图片网址,并自动命名下载到一个文件夹中
代码片段和文件信息
#-*- coding:utf-8 -*-
import re
import requests
def dowmloadPic(htmlkeywordstartNum):
pic_url = re.findall(‘“objURL“:“(.*?)“‘htmlre.S)
num = len(pic_url)
i = startNum
print(‘找到关键词:‘+keyword+‘的图片%d 张,现在开始下载图片...‘ % num)
for each in pic_url:
print(‘正在下载第‘+str(i+1)+‘张图片,图片地址:‘+str(each))
try:
pic= requests.get(each timeout=10)
string = ‘pictures1/‘+keyword+‘_‘+str(i) + ‘.jpg‘
#resolve the problem of encode make sure that chinese name could be store
fp = open(string‘wb‘)
fp.write(pic.content)
fp.close()
# except requests.exceptions.Connectionerror:
except:
print (‘【错误】当前图片无法下载‘)
continue
i += 1
return i
if __name__ == ‘__main__‘:
# lastNum = 2464
lastNum = 0
#words可以写需要下载的图片关键字
# words = [‘胖人墨镜‘
- 上一篇:pythonympx.rar
- 下一篇:SIFT的Python 代码
评论
共有 条评论