猿人学第17题题解

1. 猿人学第17题题解

在这里插入图片描述
这道题主要考察的是对于http2.0请求的爬取措施。
现在http2.0请求使用requests可以请求成功,但是对于一些反爬手段强的反而requests无法满足需求。此时就需要用到httpx或hyper这类支持http2的库。我这里使用的是httpx。

代码贴在如下:

import httpx

url = "https://match.yuanrenxue.com/api/match/3?page=1"
headers = {
    'authority': 'match.yuanrenxue.com',
    'cookie': 'sessionid=xxxx',
    'referer': 'https://match.yuanrenxue.com/match/3',
    'user-agent': 'yuanrenxue.project',
}

client = httpx.Client(http2=True)
resp = client.get(url=url, headers=headers)
print(resp.status_code)
print(resp.text)

第二种用法:

import httpx

url = "https://match.yuanrenxue.com/api/match/3?page=1"
headers = {
    'authority': 'match.yuanrenxue.com',
    'cookie': 'sessionid=6700jbpbvuy9grrhknysq5wwp4h9z8l2',
    'referer': 'https://match.yuanrenxue.com/match/3',
    'user-agent': 'yuanrenxue.project',
}

with httpx.Client(headers=headers,http2=True) as client:
    response = client.get(url)
    result = response.json()
    print(result)

拿下来的json数据,对其进行相应的解析即可

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值