Ajax爬取百度图片

2022-11-27 11:32:39 浏览数 (1)

Python爬取百度图片

代码语言:javascript复制
# -*- coding: utf-8 -*-
"""
Created on Sat Jan  5 01:38:24 2021

@author: kun
"""
import time
import requests
import json
import urllib

headers = {'Accept': 'text/plain, */*; q=0.01',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}


def get_info(category, num):
    url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=10506596288186331059&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=&pn={}&rn=30&'.format(
        category, category, num)

    html = requests.get(url, headers=headers)
    json_data = json.loads(html.text)
    datas = json_data['data']
    for data in datas[0:30]:
        picture_url = data['thumbURL']
        path = 'C:\picture\'
        urllib.request.urlretrieve(picture_url, path   picture_url[-23:])
        print("下载成功", picture_url[-23:])
        time.sleep(1)


if __name__ == '__main__':
    get_info('风景', 20)
    time.sleep(1)

结果如下:

到这里就结束了,如果对你有帮助你,欢迎点赞关注,你的点赞对我很重要

0 人点赞