微博热搜舆情监督

文章导语

目前全国防疫战还正在紧张阶段,为了避免感染人数再次反弹,大街小巷都不让出门,学校也迟迟没法开学,哎…,下周就要开始在家上网课了。平时也不怎么看微博,但是由于这次疫情,对微博的消息关注的稍微多一点,我浏览微博都只看评论,从来不发表什么意见和个人看法,刚好也好久没写过爬虫了,就当练练手了。这个过程有点小曲折,真是撸码半小时,分析十年功。

功能实现

此脚本主要的功能是获取微博50条热搜并可交互式获取相应每条热搜的评论,然后通过词云分词进行可视化,以便了解大众对热搜词条的总体态度。

实现过程
# -*- coding:utf-8 -*-
# /usr/bin/env python

"""
Author: Zhu Huaren
Date: 2020/2/25 15:29
"""

import requests
import urllib
import time
import json
import re
import jieba
import wordcloud

headers = {
    "authority": "m.weibo.cn",
    "method": "GET",
    "path": "/p/index?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=pos%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30%26display_time%3D1582790041&luicode=10000011&lfid=231583",
    "scheme": "https",
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-CN,zh;q=0.9",
    "cookie": "cokie",
    "dnt": "1",
    "referer": "https://m.weibo.cn/sw.js",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "same-origin",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36",
}
proxy = {
    'HTTP': 'HTTP://180.125.70.78:9999',
    'HTTP': 'HTTP://117.90.4.230:9999',
    'HTTP': 'HTTP://111.77.196.229:9999',
    'HTTP': 'HTTP://111.177.183.57:9999',
    'HTTP': 'HTTP://123.55.98.146:9999',

# 构造微博热搜首页api
hot_search_api = "https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=pos%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30%26display_time%3D{}&luicode=10000011&lfid=231583".format(int(time.time()))

res = requests.get(
    "https://m.weibo.cn/api/container/getIndex?containerid=100103type%3D1%26t%3D10%26q%3D%E9%92%9F%E5%8D%97%E5%B1%B1+%E8%BA%AB%E4%BD%93%E9%87%8C%E5%87%BA%E7%8E%B0%E8%B6%B3%E5%A4%9F%E6%8A%97%E4%BD%93%E4%B8%8D%E4%BC%9A%E5%86%8D%E6%84%9F%E6%9F%93&isnewpage=1&extparam=cate%3D0%26pos%3D0%26realpos%3D1%26flag%3D2%26filter_type%3Drealtimehot%26c_type%3D31%26display_time%3D1582790042&luicode=10000011&lfid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&page_type=searchall", headers=headers, proxies=proxy)
    
# 检测连接响应状态
print(res.status_code)

# 存储热搜文本和链接
hot_search_list = []
hot_search_content = requests.get(hot_search_api, headers=headers, proxies=proxy).json()
for i in range(50):
    """获取热搜文本和链接"""
    hot_search_dict = {}
    hot_search_text = hot_search_content["data"]["cards"][0]["card_group"][i]["desc"]
    hot_search_link = hot_search_content["data"]["cards"][0]["card_group"][i]["scheme"]
    hot_search_dict["TEXT"] = hot_search_text
    hot_search_dict["LINK"] = hot_search_link
    # 存储获取的热搜文本和链接
    hot_search_list.append(hot_search_dict)
    print("第【{}】条热搜:".format(i) + hot_search_text, end="\n")

# 根据需要输入对应的热搜序列
search_num = int(input("请输入序列号(范围:0-50):"))
search = hot_search_list[search_num]["TEXT"]
print("您要搜索的热搜是:【" + search + "】")

api_url_list = []
# 解析每个热搜若干页的url
parse_text = urllib.parse.quote("type=60&q=#" + search + "#&t=10&title=热门-#" + search + "#")
parse_text_ = urllib.parse.quote("title=热门&mid=&q=#" + search + "#")
parse_text__ = urllib.parse.quote("type=1&t=10&q=#" + search + "#")

# "20" 可以根据评论数量进行调整
for n in range(20):
    api_url_dict = {}
    parse_link = "https://m.weibo.cn/api/container/getIndex?containerid=100103" + parse_text + "&cardid=weibo_page&extparam=" + parse_text_ +"&luicode=10000011&lfid=100103" + parse_text__ + "&page={}".format(n)
    api_url_dict["【" + search + "】" + "热搜第{}页API".format(n)] = parse_link
    api_url_list.append(api_url_dict)
    # print(parse_link)

# 存储每个微博的id
blog_id_nums = []
for v in range(len(api_url_list)):
    mblog_res = requests.get(list(api_url_list[v].values())[0], headers=headers, proxies=proxy)
    # print(a)
    mblog_res_json = mblog_res.json()
    for blog_id in range(len(mblog_res_json["data"]["cards"])):
        try:
            blog_id_num = mblog_res_json["data"]["cards"][blog_id]["mblog"]["id"]
            blog_id_nums.append(blog_id_num)
        except KeyError:
            pass
# print(blog_id_nums)

get_data_list = []
def get_data(url):
    res_json = requests.get(url, headers=headers, proxies=proxy).json()
    # print(len(res_json["data"]["data"]))
    for p in range(len(res_json["data"]["data"])):
        try:
            comment_text = res_json["data"]["data"][p]["text"]
            # print(comment_text)
            cleaned_text = re.sub('<[^<]+?>', '', comment_text).replace('\n', '').strip()
            get_data_list.append(cleaned_text)
            print(get_data_list)
        except KeyError:
            pass

for j in range(len(blog_id_nums)):
    blog_id = blog_id_nums[j]
    base_comment_url = "https://m.weibo.cn/comments/hotflow?id={}&mid={}&max_id_type=0".format(blog_id, blog_id)

    try:
        base_comment_url_res = get_data(base_comment_url)
        base_max_id_json = requests.get(base_comment_url, headers=headers, proxies=proxy).json()
        # print(base_max_id_json)
        base_max_id = base_max_id_json["data"]["max_id"]
        # print(base_max_id)
        comment_url = "https://m.weibo.cn/comments/hotflow?id={}&mid={}&max_id={}&max_id_type=0".format(blog_id,
                                                                                                        blog_id,
                                                                                                        base_max_id)
        comment_urlres = get_data(comment_url)
    except Exception:
        pass

def split_words(text):
    text_ = ''.join(text)
    cut_text = jieba.cut(text_)
    string = ','.join(cut_text)
    # stop_words = ['str']
    word_cloud = wordcloud.WordCloud(
        font_path=r'.\simhei.ttf',
        background_color='white',
        width=500,
        height=350,
        max_font_size=100,
        min_font_size=10,
        # stopwords=stop_words,
        scale=15,
    )
    word_cloud.generate(string)
    word_cloud.to_file(r'{}.png'.format(search))
split_words(text=get_data_list)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

文章结语

中华民族自古以来就是多灾多难的民族,我们的祖辈们经历过各种各样的困难,这次疫情也只不过是我们中华民族几千年历史中的一抹灰色,在这场中国人民的战争,乃至世界人民的战争中,我们一定会取得最后的胜利,我们终将胜利!
武汉加油!中国加油!世界加油!

百度百科创建词条工具V2.7 我们先了解一下百度百科的优势: 1.权威性,广大网民对百度百科信任度特别高,容易受百科中相关信息影响 2.排名好,绝大部分百科词条(即关键词)能排至各大索引擎的前三位 3.流量大,一般词条每天浏览量不亚于一个中型企业站每天的总流量 4.转化率高,百科成为网民上网查资料必看的网站,转化为客户的几率较PPC及PM广告大得多。如病人及其家属查找某疾病如何治疗时,必看百科 5.长期有效,百科广告加上之后长期稳定有效,且不产生后续费用 ………   那么,百度百科创建词条工具可以提供哪些帮助呢? 1.创建百科词条,如品牌名、网站名、产品词、人名、公司名… 2.修改百科词条,在原有词条中加入您的内容,如广告内容、名片… 3.删除百科词条,删除不利词条内容或整个词条(不推荐使用) 4.百科内容撰写,根据客户要求编写词条内容 如有其他需求可及时与客服人员沟通,QQ:120962274 百度百科创建词条网站:www.uducn.com   百度百科创建词条常见错误: 1.修改词条原因不明确。例如:修改原因为“编辑词条”“不具体”“更完善”“更具体”等。 2.修改词条原因错误。例如:修改内容是添加图片,填写的修改原因为添加链接。 3.修改词条原因未能明确说明修改的具体区域。例如:修改了词条中的内容并添加了链接,修改原因应写明:添加内容以及链接,必须指出修改或删除的错误内容,并给出具体理由;只修改错别字,必须指明具体的错字;若您修改了表格中的内容,必须明确指出您修改了表格中的哪部分内容。 4.修改词条不可完全删除原词条的内容,可以选择性删除修改原词条内容。 5.编辑者误将角标误添加在段首,或误添加在了完整段落句号之前。   百度百科创建词条时请注意您的词条中不要有违背“百科原则”的内容,否则词条将被编辑删除,并扣除20分,情节严重者,“百科”有权对其做出关闭部分权限、暂停直至删除其帐号等处罚。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值