微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/ajax/162340.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐