requests模拟ajax异步请求

前端之家收集整理的这篇文章主要介绍了requests模拟ajax异步请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

以请求知乎主题广场为例子

1. 观察数据

2. 分析

构造请求

import requests
def get_sub_topics(url,topic_id,offset):
    """获取某个topic下的子topic topic_id :某个主题的id offset :下拉刷新时获取的条数,一次下拉刷新offset增加20 """
    params = '{0}{4}topic_id{4}:{1},{4}offset{4}:{2},{4}hash_id{4}:{4}{4}{3}'.format('{',offset,'}','"')
    data = {
        "method":"next","params": params
    }
    print(data['params'])
    req = requests.post(SUB_TOPIC_HTTP,data=data,proxies=proxies,headers=REQUEST_HEADERS)
    json_data = req.json()
    sub_topics = json_data['msg']
    print(sub_topics[0])
    print(sub_topics[2])

结果

https://www.zhihu.com/topics#美食 304
{"topic_id":304,"offset":20,"hash_id":""}
<div class="item"><div class="blk">
<a target="_blank" href="/topic/19579555">
<img src="https://pic3.zhimg.com/4ab91208a_xs.jpg" alt="川菜">
<strong>川菜</strong>
</a>
<p>川菜作为中国汉族四大菜系之一,取材广泛,调味多变,菜式多样,口…</p>

<a id="t::-9766" href="javascript:;" class="follow Meta-item zg-follow"><i class="z-icon-follow"></i>关注</a>

</div></div>
<div class="item"><div class="blk">
<a target="_blank" href="/topic/19552371">
<img src="https://pic1.zhimg.com/183ecd6c8_xs.jpg" alt="巧克力">
<strong>巧克力</strong>
</a>
<p>巧克力(Chocolate)是以可可做为主料的一种混合型食品,…</p>

<a id="t::-721" href="javascript:;" class="follow Meta-item zg-follow"><i class="z-icon-follow"></i>关注</a>

</div></div>
原文链接:https://www.f2er.com/ajax/162340.html

猜你在找的Ajax相关文章