主页 M

python爬百度图片并保存,提交多数据

2020-03-16 网页编程网 网页编程网
# -*- coding: utf-8 -*-
import requests
from urllib import request

url='https://image.baidu.com/search/index?ct=201326592&cl=2&st=-1&lm=-1&nc=1&ie=utf-8&tn=baiduimage&ipn=r&rps=1&pv=&fm=rs1&word=%E5%B9%BF%E8%A5%BF%E6%A1%82%E6%9E%97%E7%BE%8E%E5%A5%B3&oriquery=%E7%BE%8E%E5%A5%B3&ofr=%E7%BE%8E%E5%A5%B3&sensitive=0'
headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4068.4 Safari/537.36'
        }
data_str='''
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: 
hd: 
latest: 
copyright: 
word: 广西桂林美女
s: 
se: 
tab: 
width: 
height: 
face: 
istype: 
qc: 
nc: 1
fr: 
expermode: 
force: 
pn: 60
rn: 30
gsm: 3c
1584341831956:'''
send_data={}
#按行分隔
for line in  data_str.splitlines():
    line_data=line.split(': ')
    if len(line_data)==2:
        #序列解包赋值
        key,value=line_data
        if key and value:
            send_data[key]=value

#模拟浏览器
response=requests.get(url=url,headers=headers,params=send_data)            
print(response.text)
#是一些字符串,现要转成字典queryEnc,列表data
content=response.json()["data"]
num=1
for src in content:
   num+=1
   img_url=src.get('middleURL')

    #若为none不下载,只下载正确的url
    #先在本地写个文件
   if img_url:
      name='image/image_%s_%s.png'%('广西桂林美女',num)
      try:
          request.urlretrieve(url=img_url,filename=name)
      except Exception as e:
          print(e)
      else:
          print('%s downed'%name)
阅读原文
阅读 3377
123 显示电脑版