彭于晏百度图片Ajax异步加载爬取

Ajax 异步加载 网页爬取

分析网页

在Networka条目里找xhr
Ajax 加载

import requests
from urllib.parse import urlencode
import re

headers = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36’,
‘X-Requested-With’: ‘XMLHttpRequest’
}
def get_page(url):
“”“获取网页源码”""

response = requests.get(url,headers = headers)
response.encoding = 'utf-8'
if response.status_code == 200:
    print(response)
    return response.json()
else:
    print('错误:',response.status_code)

def parse_page(json):

data = json.get('data')
if data:
    for item in data:
        image = item.get('hoverURL')
        name = item.get('fromPageTitleEnc')
        print(name)
        print(image)



        if image != None :
            reg = "[^0-9A-Za-z\u4e00-\u9fa5]"           #使用删除字符串里的符号   ?!,。之类的   保留允许存在的命名的字符串
            names = re.sub(reg, '', name)
            images = requests.get(image).content
            with open('./images/%s'%names+'.jpg', 'wb') as fp:
                    fp.write(images)
        else:
            print('None')

def main(pn):

data = {
    "tn": "resultjson_com",  # 这里不能有空格  否则会打印的是其他的网页源码
    "ipn": " rj",
    "ct": "201326592",
    "queryWord": "彭于晏图片",
    "ie": "utf-8",
    "oe": "utf-8",
    "adpicid": "",
    "copyright": "",
    "word": " 彭于晏图片",
    "pn": pn,
}
url = "https://image.baidu.com/search/acjson?" + urlencode(data)
json = get_page(url)
parse_page(json)

if name == ‘main’:
pn = 30
for i in range(4):
main(pn * i)

        # print(main)
        # print(pn)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值