import requests
import os
def getManyPages(keyword,pages):
params=[]
for i in range(30,30*pages+30,30):
params.append({
'tn': 'resultjson_com',
'ipn': 'rj',
'ct': 201326592,
'is': '',
'fp': 'result',
'queryWord': keyword,
'cl': 2,
'lm': -1,
'ie': 'utf-8',
'oe': 'utf-8',
'adpicid': '',
'st': -1,
'z': '',
'ic': 0,
'word': keyword,
's': '',
'se': '',
'tab': '',
'width': '',
'height': '',
'face': 0,
'istype': 2,
'qc': '',
'nc': 1,
'fr': '',
'pn': i,
'rn': 30,
'gsm': '1e',
'1488942260214': ''
})
url = 'https://image.baidu.com/search/acjson'
urls = []
for i in params:
urls.append(requests.get(url,params=i).json().get('data'))
return urls
def getImg(dataList, localPath):
if not os.path.exists(localPath): # 建立檔案夾
os.mkdir(localPath)
x = 0
for list in dataList:
for i in list:
if i.get('thumbURL') != None:
print('正在下載下傳:%s' % i.get('thumbURL'))
ir = requests.get(i.get('thumbURL'))
open(localPath + '%d.jpg' % x, 'wb').write(ir.content)
x += 1
else:
print('圖檔連結不存在')
if __name__ == '__main__':
dataList = getManyPages('插畫',10) # 參數1:關鍵字,參數2:要下載下傳的頁數
getImg(dataList,'C:\\Users\\0\\Pictures\\插畫\\photos\\') # 參數2:指定儲存的路徑
報錯:
Python ImportError: No module named 'requests'解決方法
缺少requests庫
解決方法:由于我安裝的python的時候,也選擇安裝了pip,是以這裡隻分享自己實踐過的方式。我的python安裝的目錄是D:/Python
①cmd
②cd C:\Users\0\AppData\Local\Programs\Python\Python36
③pip install requests
等待系統自動加載安裝。