美文网首页
python ajax weibo

python ajax weibo

作者: Al_不期而遇 | 来源:发表于2019-01-13 12:23 被阅读7次

import requests

from urllib.parse import urlencode

from pyquery import PyQuery as pq

base_url = 'https://m.weibo.cn/api/container/getIndex?'

headers = {

'Host':'m.weibo.cn',

'Refere':'https://m.weibo.cn/u/2830678474',

'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:59.0) Gecko/20100101 Firefox/59.0',

'X-Requested-With':'XNLHttpRequest',

}

def get_page(page):

params = {

'type' : 'uid',

'value' : '2830678474',

'containerid' : '1076032830678474',

'page' : page

}

url = base_url + urlencode(params)

try:

response = requests.get(url, headers=headers)

if response.status_code == 200:

return response.json()

except requests.ConnectionError as e:

print('Error', e.args)

def parse_page(json):

    if json:

        items = json.get('data').get('cards')

        for item in items:

            item = item.get('mblog')

            weibo = {}

weibo['id'] = item.get('id')

            weibo['text'] = pq(item.get('text')).text()

            weibo['attitudes'] = item.get('attitudes_count')

            weibo['comments'] = item.get('comments_count')

            weibo['reposts'] = item.get('reposts_count')

            yield weibo

if __name__ == '__main__':

for page in range(1, 87):

json = get_page(page)

# results = parse_page(page)

results = parse_page(json)

for result in results:

print(result)

相关文章

  • python ajax weibo

    import requests from urllib.parse import urlencode from p...

  • 2019-02-27问题(三)

    题目来源:python常见面试题——网络编程和前端部分 题目: 1. AJAX是什么,如何使用AJAX? ...

  • 2018-06-21 返回json格式的数据

    def ajax_dict(request): name_dict = {'twz':'Love python...

  • 转weibo

    什么样的人算是独立的人? 知乎上的一个回答是:一个人有自己的价值观并能根据自己的价值观做出人生选择,并且言行一致能...

  • Weibo 2017.6.25

    我最近总是有一个很恐怖的想法 烦躁的时候就预谋着怎么自杀 虽然很蠢 说出来很傻逼 但是我说出来的原因是我微博没有...

  • Weibo Is Not Trustworthy

    Starting 2010, Weibo has become a news reader and note-ta...

  • 关于weibo

    卸掉weibo有一点失落,像是告别了自己的爱情,总是习惯性想去打开它,其实并没有什么好看的有趣的,里面变得全是八卦...

  • weibo崩了

    已经刷不出任何新动态了,不知道什么时候能恢复正常。 好不容易过了半个小时终于能刷出动态了,还没正常五分钟又崩了。不...

  • Python与AJAX

    Ajax: jquery提供的ajax功能: success的匿名函数function要等到服务端返回来的时候,才...

  • Ajax小示例

    以前用的Ajax实例,以后用的时候可以仿照写 JS部分 Python代码

网友评论

      本文标题:python ajax weibo

      本文链接:https://www.haomeiwen.com/subject/qccuuftx.html