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>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...