前言
双减是指有效减轻义务教育阶段学生过重作业负担和校外培训负担。
2021年10月,全国人大表示:双减拟明确入法,避免加重义务教育阶段学生负担。 2021年11月3日,市场监管总局等八部门发布《关于做好校外培训广告管控的通知》。坚决杜绝地铁、公交站台等所属广告牌、广告位刊发校外培训广告。
一、数据来源
选择微博中关于“双减”政策的热门话题,如下图所示
二、数据爬取
代码如下(示例):
# encoding=utf8
import requests
import pandas as pd
import json
import time
import re
# 设置头部和cookie,反爬,伪装
header = {
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}
Cookie = {
'Cookie': '' #此处放置自己的Cookie
}
# 评论翻页的关键字段
max_id = ""
# 设置循环
while True:
# 评论第一页max_id为空值
if max_id == "":
url = "https://m.weibo.cn/comments/hotflow?id=4662583791456072&mid=4662583791456072&max_id_type=0"
else:
# 显示max_id
print(max_id)
# 评论后一页url中的max_id为前一页传递来的参数
url = "https://m.weibo.cn/comments/hotflow?id=4662583791456072&mid=4662583791456072&max_id=" + str(
max_id) + "&max_id_type=" + str(max_id_type)
print("请求的url是:" + url)
# request对象获取
response = requests.get(url, headers=header, cookies=Cookie)
# json格式解析
comment = response.json()
print("requestion请求状态:" + str(comment["ok"]))
# 如果Ok值为1,表示解析成功
if comment["ok"] == 0:
break
# 获取max_id值
max_id = comment["data"]["max_id"]
max_id_type = comment["data"]["max_id_type"]
print("max_id is:" + str(max_id))
print("max_id_type is:" + str(comment["data"]["max_id_type"]))
# 获取评论文本,并过滤符号和英文字符
for comment_data in comment["data"]["data"]:
data = comment_data["text"]
p = re.compile(r'(<span.*>.*</span>)*(<a.*>.*</ a>)?')
data = re.sub('[^\u4e00-\u9fa5]', '', data)
data = p.sub(r'', data)
data1 = [(comment_data['created_at'], comment_data['user']['id'], comment_data['user']['screen_name'], data)]
data2 = pd.DataFrame(data1)
data2.to_csv('weibo_comment.csv', header=False, index=False, mode='a+')
# 休眠3秒,防止被系统认为是爬虫
time.sleep(3)
三、数据清洗
1.清除缺失数据
2.去除重复数据
3