# !/user/bin/python3
# -*- coding:utf-8 -*-
import requests
import os
import re # 使用正则表达式
def getManyPages(keyword, pages):
params=[]
# 分页请求地址拼接 每页显示60个图片
for i in range(0, 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 baidtu_uncomplie(url):
res = ''
c = ['_z2C$q', '_z&e3B', 'AzdH3F']
d= {'w':'a', 'k':'b', 'v':'c', '1':'d', 'j':'e', 'u':'f', '2':'g', 'i':'h', 't':'i', '3':'j', 'h':'k', 's':'l', '4':'m', 'g':'n', '5':'o', 'r':'p', 'q':'q', '6':'r', 'f':'s', 'p':'t', '7':'u', 'e':'v', 'o':'w', '8':'1', 'd':'2', 'n':'3', '9':'4', 'c':'5', 'm':'6', '0':'7', 'b':'8', 'l':'9', 'a':'0', '_z2C$q':':', '_z&e3B':'.', 'AzdH3F':'/'}
if(url==None or 'http' in url):
return url
else:
j= url
for m in c:
j=j.replace(m,d[m])
for char in j:
if re.match('^[a-w\d]+$',char):
char = d[char]
res= res+char
return res
# 根据路径和图片地址列表下载图到本地
def getImg(dataList, localPath):
if not os.path.exists(localPath): # 新建文件夹
os.mkdir(localPath)
x = 1
for list in dataList:
for i in list:
if i.get('objURL') != None:
print('正在下载:%s' % baidtu_uncomplie(i.get('objURL')))
try:
ir = requests.get(baidtu_uncomplie(i.get('objURL')))
open(localPath + '%d.jpg' % x, 'wb').write(ir.content)
x += 1
except Exception as e:
print(e)
else:
print('图片链接不存在')
if __name__ == '__main__':
# 参数1:关键字,参数2:要下载的页数
dataList = getManyPages('小清新壁纸', 1)
print(dataList)
# 参数2:指定保存的路径
getImg(dataList, 'D:/a/')