資源簡介
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 代碼
評論
共有 條評論