爬虫(二):百度图片爬取

  • 很久以前写的爬虫,就不介绍具体的步骤
import os
import requests

pic_num = 0
keyword = ""
datas = []


def get_page_html(url, keyword, index):
    data = {
        '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': index,
        'rn': 30,
        'gsm': '1e',
        '1488942260214': ''
    }
    data = requests.get(url, params=data).json().get('data')
    return data


def create_page_file(file_name):
    if not os.path.exists(file_name):
        os.mkdir(file_name)
    else:
        print("文件夹已经存在...")


def get_page_image(datas, file_name):
    num = 0
    for data in datas:
        for content in data:
            pic_url = content.get("thumbURL")
            if pic_url is not None:
                print("正在下载:", pic_url)
                pic_data = requests.get(pic_url)
                string = file_name + r'\\' + keyword + '-' + str(num) + '.jpg'
                with open(string, 'wb') as fp:
                    fp.write(pic_data.content)
                    fp.close()
                num += 1
                if num >= pic_num:
                    return


if __name__ == "__main__":
    keyword = input("请输入所要获取的内容:")
    pic_num = int(input("获取的图片个数:"))
    for i in range(30, pic_num, 30):
        datas.append(get_page_html('https://image.baidu.com/search/acjson?', keyword=keyword, index=pic_num))
    file_name = input("请输入存放文件夹的名字:")
    create_page_file(file_name=file_name)
    get_page_image(datas, file_name)
上一篇:运用iGuard防御ADS权限维持


下一篇:王者战力查询接口(图文版)