python爬虫:百度图片爬虫代码

代码里的logid我也不确定有没有时效,如果有的话请大家自行替换,就在数据包标头那里,如图

python爬虫:百度图片爬虫代码_第1张图片

代码来了,来了,来了.......

详细解说在这里:百度图片爬虫代码详解

#Author:Griffy
#Date:2021-09-25
#Description:爬百度图片,只留下了堆糖的图,不想要过滤的把下面有一个filter删掉即可,还有if条件判断
#Version:1.0    以后可能会更新一下,可能吧

import requests
import re
import PySimpleGUI as sg
import os

def  baidtu_uncomplie(url):                                 #因为要获取的是objURL所以这里找了一个百度解密
    res = ''
    c = ['_z2C$q', '_z&e3B', 'AzdH3F']
    d= {'w':'a', 'k':'b', 'v':'c', '1':'d', 'j':'e', 'u':'f', '2':'g', 'i':'h', 't':'i', '3':'j', 'h':'k', 's':'l', '4':'m', 'g':'n', '5':'o', 'r':'p', 'q':'q', '6':'r', 'f':'s', 'p':'t', '7':'u', 'e':'v', 'o':'w', '8':'1', 'd':'2', 'n':'3', '9':'4', 'c':'5', 'm':'6', '0':'7', 'b':'8', 'l':'9', 'a':'0', '_z2C$q':':', '_z&e3B':'.', 'AzdH3F':'/'}
    if(url==None or 'http' in url):
        return url
    else:
        j= url
        for m in c:
            j=j.replace(m,d[m])
        for char in j:
            if re.match('^[a-w\d]+$',char):
                char = d[char]
            res= res+char
        return res

layout=[                                             #弹窗设计,让用户进行主题输入
    [sg.Text('请输入你要寻找的图片主题')],
    [sg.Input()],
    [sg.Text('请输入你需要的图片数(请填写阿拉伯数字,例如:1):')],
    [sg.Input()],
    [sg.Button('确认')]
    ]
window=sg.Window('百度图片下载',layout)
event,values=window.read()
window.close()

url='https://image.baidu.com/search/acjson'
for i in range(0,180,30):
    params={
        'tn': 'resultjson_com',
        'logid': '5280191307193676757',  #不知道这里有没有时效
        'ipn': 'rj',
        'ct': '201326592',
        'fp': 'result',
        'queryWord': values[0],
        'cl': '2',
        'lm': '-1',
        'ie': 'utf-8',
        'oe': 'utf-8',
        'st': '-1',
        'ic': '0',
        'word': values[0],
        'face': '0',
        'istype': '2',
        'nc': '1',
        'pn': i,
        'rn': '30',
        'gsm': 'b4'
    }
    headers={                            #遇到Forbid spider access就在请求头多加一些东西,试了一下'Accept-Encoding'是关键
        # 'Accept': 'text/plain, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, br',
        # 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        # 'Connection': 'keep-alive',
        'Cookie': 'BDqhfp=%E5%8F%B2%E5%8A%AA%E6%AF%94%26%260-10-1undefined%26%263746%26%264; BIDUPSID=5A04241009FD166564DACF4050551F2D; PSTM=1611376447; __yjs_duid=1_2de46e288096c13a7edea3d05a5204421620544039468; BAIDUID=F163DBC1DF098AF604AE753E72659BAA:FG=1; BDUSS=Tczc3RnamRhaUhicm5rfm83V3pkMTBySUd1Z0V4Q25mbXhYdElRemJHdVktVnRoRVFBQUFBJCQAAAAAAQAAAAEAAACR090iR3JpZmZleTUxMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJhsNGGYbDRhN; BDUSS_BFESS=Tczc3RnamRhaUhicm5rfm83V3pkMTBySUd1Z0V4Q25mbXhYdElRemJHdVktVnRoRVFBQUFBJCQAAAAAAQAAAAEAAACR090iR3JpZmZleTUxMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJhsNGGYbDRhN; BAIDUID_BFESS=F163DBC1DF098AF604AE753E72659BAA:FG=1; BDORZ=AE84CDB3A529C0F8A2B9DCDD1D18B695; userFrom=cn.bing.com; IMG_WH=573_756; H_WISE_SIDS=110085_178384_179349_181588_182531_183327_183611_183750_184578_185029_185517_185653_185750_186317_186411_187020_187195_187206_187292_187450_187663_187670_187928_8000097_8000100_8000126_8000140_8000150_8000169_8000177_8000185; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; firstShowTip=1; cleanHistoryStatus=0; indexPageSugList=%5B%22%E5%8F%B2%E5%8A%AA%E6%AF%94%22%2C%22%E6%B5%B7%E8%B4%BC%E7%8E%8B%22%2C%22%E8%8A%B1%E6%9C%B5%22%2C%22undertale%22%2C%22python%22%2C%22%E8%8A%B1%E7%AE%80%E7%AC%94%E7%94%BB%22%2C%22%E5%90%8C%E5%9E%8B%E4%B8%96%E4%BB%A3%E4%BA%A4%E6%9B%BF%22%2C%22%E6%A8%9F%E7%A7%91%E8%8A%B1%22%2C%22%E6%A8%9F%E7%A7%91%E8%8A%B1%E7%9A%84%E8%A7%A3%E5%89%96%22%5D; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; ab_sr=1.0.1_MTY1OTI2YjEyMzViNzQyYWVhZjdhZWQxNzc0YjE1NzA2NGEyZmMwZGEwNzRmMWVjZGM3N2IzMDlkYjViZWVlOGYyNTllZDMzZjgwZGMxZWZhOWFiMmEyYjg0NjgyYzgwYjk0Y2QxYWVmM2E1ZTFiZjkyYTNlOGYzMDg1MWVjNjUyODViYzMyZjc2Mjk2OGFmZmZjZTkwNjg3OWI4NjhjZjdiNzJmNTY3NTIyZjg2ODVjMzUzNTExYjhiMjkxZjEx; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm',
        'Referer': 'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111110&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%8F%B2%E5%8A%AA%E6%AF%94&oq=%E5%8F%B2%E5%8A%AA%E6%AF%94&rsp=-1',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Mobile Safari/537.36 Edg/93.0.961.52'
    }
    html=requests.get(url,params=params,headers=headers)
    fin=re.compile(' "objURL":"(.*?)"')
    filter='ssl.duitang.com'
    results=fin.findall(html.text)
    num=1
    for r in results:
        if filter in baidtu_uncomplie(r) and num <= int(values[1]):
            pic_url=baidtu_uncomplie(r)
            filename=f"C:\{values[0]}"
            if not os.path.exists(filename):        #创建文件夹来保存图片
                os.mkdir(filename)
            with open(f"{filename}/{num}.jpg",'wb') as f:        #二进制文件的保存方法
                f.write(requests.get(pic_url).content)

            num+=1

sg.popup('已完成!图片文件夹已经放入C盘!')



你可能感兴趣的:(网络爬虫,python,百度,爬虫)