您好,登錄后才能下訂單哦!
自己寫了玩的一個小腳本,百度圖片下載
import re import os import requests import hashlib def dowmloadPic(html, keyword): pic_url = re.findall('"objURL":"(.*?)",', html, re.S) if len(pic_url) < 1: return 1 i = 0 for each in pic_url: print(i + 1, end=',') md5Str = hashlib.md5(each.encode("utf-8")).hexdigest() # 抓去鏈接 oneStr = md5Str + ' ' + keyword + ' ' + each + '\n' with open('downText.txt', 'a+') as f: f.write(oneStr) # 下載圖片 # try: # pic = requests.get(each, timeout=10) # except requests.exceptions.ConnectionError: # print('鏈接超時,跳過此操作') # continue # # kz = os.path.splitext(each)[-1] # photo = + keyword + '_' + str(i) + kz # # with open(photo, 'wb') as f: # f.write(pic.content) i += 1 print('\n') return 0 if __name__ == '__main__': word = input('enter a key word:') page = input('enter the page:') page = int(page) page = 1 if page < 1 else page url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&ct=201965323&v=flip' p = 1 while (p <= page): print(word + ',第[' + str(p) + ']頁:') pn = (p - 1) * 20 url = url + '&pn=' + str(pn) result = requests.get(url).content.decode('utf-8') code = dowmloadPic(result, word) if code: print('無相關數據,提前退出程序') break p = p + 1 print('程序結束')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。