Python3抓取新浪微博热搜榜

一、分析这个页面的地址

F12 进network查看发送的xhr请求。

发现有ajax请求。https://weibo.com/ajax/side/hotSearch,返回的正是热搜的json数据。

二、分析接口

三、代码

import requests
import json


weibohot_url = 'https://weibo.com/ajax/side/hotSearch'
response = requests.get(weibohot_url)
r = json.loads(response.text)
if r['ok']==1 :
    data = r['data']['realtime']
    for item in data:
        # 标题
        title = item['word']
        print('标题为:{}'.format(title))
        # 链接
        url = "https://s.weibo.com/weibo?q=%23{}%23".format(title)
        print('链接为:{}'.format(url))
        # 排名
        rank = item['rank'] + 1
        print('排名为:{}'.format(rank))
        # 指数
        zhishu = item['num']
        print('指数为:{}'.format(zhishu))

You May Also Like

About the Author: 萌新

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注