Python爬取百度壁纸图片

Python爬取百度壁纸图片
#! /usr/bin/python

-- coding: utf-8 --

@Author : declan

@Time : 2020/05/31 16:29

@File : fun_baidu.py

@Software: PyCharm

from urllib import request
import requests,os,time
def get_image(keywords,num):
url=‘https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%A3%81%E7%BA%B8&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&word=%E5%A3%81%E7%BA%B8&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&cg=wallpaper&pn=30&rn=30&gsm=1e&1590913895971=’
headers={
“Referer”:“https: // image.baidu.com / search / index?tn = baiduimage & ipn = r & ct = 201326592 & cl = 2 & lm = -1 & st = -1 & sf = 1 & fmq = & pv = & ic = 0 & nc = 1 & z = & se = 1 & showtab = 0 & fb = 0 & width = & height = & face = 0 & istype = 2 & ie = utf - 8 & fm = index & pos = history & word = % E5 % A3 % 81 % E7 % BA % B8”,
“User-Agent”:“Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.9 Safari/537.36”,
}
##以下不能存在空格
str_data=’’’
tn: resultjson_com
ipn: rj
ct: 201326592
is:
fp: result
queryWord: 壁纸
cl: 2
lm: -1
ie: utf-8
oe: utf-8
adpicid:
st: -1
z:
ic: 0
hd:
latest:
copyright:
word: 壁纸
s:
se:
tab:
width:
height:
face: 0
istype: 2
qc:
nc: 1
fr:
expermode:
force:
cg: wallpaper
pn: 30
rn: 30
gsm: 1e
1590913895971:
‘’’
sendData={}
send_data=str_data.splitlines()
try:
for i in send_data:
data_list=i.split(’: ‘)
if len(data_list)==2:
key,value=data_list
if key and value:
sendData[key]=value
#print(sendData)
except Exception as e:
print(e)
sendData[‘word’]=sendData[‘queryWord’]=keywords
sendData[‘pn’]=str(30*num)
#print(sendData)
response = requests.get(url=url,headers=headers,params=sendData)
content = response.json()[“data”]
#print(content)
for index,src in enumerate(content):
img_url = src.get(‘middleURL’) #以键取值,获取字典中指
#img_url=src[‘middleURL’]
#print(img_url)
if os.path.exists(‘image’):
pass
else:
os.mkdir(‘image’)
if img_url and os.path.exists(‘image’):
name=’./image/image_%s_%s.jpg’%(keywords,index+1)
try:
request.urlretrieve(url=img_url,filename=name)
except Exception as e:
print(e)
else:
print(’%s is download’%name)
time.sleep(1)
if name == ‘main’:
keywords=input(“请输入你要的内容:”)
num=int(input(‘请输入你想要的数量:’)) #输入的数字要转化为int类型
get_image(keywords,num)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值