import requests
url ="https://image.baidu.com/search/acjson?tn=resultjson_com&logid=7266558810577433352&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E9%98%BF%E5%87%A1%E8%BE%BE&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=©right=&word=%E9%98%BF%E5%87%A1%E8%BE%BE&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn=60&rn=30&gsm=3c&1613315170875="
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
resp = requests.get(url, headers=headers)
resp_json = resp.json()
data_list = resp_json['data']
lst = []
for item in data_list:
if len(item) != 0:
lst.append(item['thumbURL'])
for x in lst:
print(x)
import requests
import uuid
import random
import time
url ="https://image.baidu.com/search/acjson?tn=resultjson_com&logid=7266558810577433352&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E9%98%BF%E5%87%A1%E8%BE%BE&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=©right=&word=%E9%98%BF%E5%87%A1%E8%BE%BE&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn=60&rn=30&gsm=3c&1613315170875="
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
resp = requests.get(url, headers=headers)
resp_json = resp.json()
data_list = resp_json['data']
lst = []
for item in data_list:
if len(item) != 0:
lst.append(item['thumbURL'])
#以下为存储过程
# 遍历列表存储所有图片
for item in lst:
# 发送请求
try:
pic= requests.get(item, timeout=100)
except requests.exceptions.ConnectionError:
print ('错误:当前图片无法下载')
continue
# uuid4为图片名称
#,创建img文件夹, wb+:写入二进制数据
file=open(str.format("{0}.jpg",uuid.uuid4()), 'wb+')
file.write(pic.content)
file.close()
#每次操作完休息1~3s
timeStop=random.randint(1,4)
time.sleep(timeStop)
print("完成")
a)、有的时候你会发现,可以直接处理json数据,很爽的哦。
b)、但是大多时候还得一点点的拆。诶,任重道远啊,慢慢练吧。
欢迎【点赞】、【评论】、【关注】、【收藏】、【打赏】,为推广知识贡献力量。
本章内容应该是全网暂时下载百度图片比较靠谱的了,如果想根据搜索词下载图片,直接改参数即可。