1,获取数据
如李子染,搜索热度,什么时候开始关注
代码
import requests
import os
import csv
fp = open('data.csv','a+',newline='',encoding='utf-8')
writer = csv.writer(fp)
writer.writerow(['time'])
url_1 = 'https://m.weibo.cn/api/container/getIndex'
headers = {
'Accept':'application/json,text/plain,*/*',
'Mweibo-Pwa':'1',
'referer': 'https://m.weibo.cn/search?containerid=100103type%3D1%26q%3D%E8%8B%B1%E5%9B%BD%E7%95%99%E5%AD%A6&sudaref=m.weibo.cn&display=0&retcode=6102&page_type=searchall',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
for i in range(5,55):
url_2={
'containerid':'100103type=1&q=李子染',
"sudaref":"m.weibo.cn",
"display":"0",
"retcode":"6102",
"page_type":"searchall",
"page":str(i)
web=requests.get(url_1,params=url_2)
web_json=web.json()
content=web_json["data"]["cards"]
for item in content:
time=item["mblog"]["created_at"]
print(time)
writer.writerow([time])
2,存入csv
3,对数据的分析和统计
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv("data.csv",encoding="gbk")
#print(df[u"time"])[:0]
counts=df[u"time"].value_counts()
counts=df[u"time"].value_counts(normalize=True)
4,运行
09-12 30
12-11 28
09-13 20
11-04 18
09-14 14
12-12 14
12-14 14
12-16 11
12-17 9
12-15 9
11-02 7
12-18 7
12-07 6
12-02 6
10-18 5
12-05 5
12-13 5
09-15 5
12-08 4
11-23 4
10-31 3
10-21 3
12-19 3
11-03 3
10-13 3
09-22 3
11-20 3
12-09 3
..
09-27 1
09-24 1
10-30 1
11-01 1
11-30 1
11-07 1
11-24 1
11-28 1
09-20 1
09-11 1
10-28 1
11-21 1
10-22 1
09-16 1
11-12 1
10-23 1
09-25 1
10-14 1
11-22 1
11-26 1
11-15 1
11-09 1
10-26 1
09-28 1
11-18 1
10-12 1
10-01 1
09-19 1
09-23 1
10-11 1
Name: time, Length: 73, dtype: int64
12-10 0.126582
12-06 0.116456
09-12 0.075949
12-11 0.070886
09-13 0.050633
11-04 0.045570
09-14 0.035443
12-12 0.035443
12-14 0.035443
12-16 0.027848
12-17 0.022785
12-15 0.022785
11-02 0.017722
12-18 0.017722
12-07 0.015190
12-02 0.015190
10-18 0.012658
12-05 0.012658
12-13 0.012658
09-15 0.012658
12-08 0.010127
11-23 0.010127
10-31 0.007595
10-21 0.007595
12-19 0.007595
11-03 0.007595
10-13 0.007595
09-22 0.007595
11-20 0.007595
12-09 0.007595
...
09-27 0.002532
09-24 0.002532
10-30 0.002532
11-01 0.002532
11-30 0.002532
11-07 0.002532
11-24 0.002532
11-28 0.002532
09-20 0.002532
09-11 0.002532
10-28 0.002532
11-21 0.002532
10-22 0.002532
09-16 0.002532
11-12 0.002532
10-23 0.002532
09-25 0.002532
10-14 0.002532
11-22 0.002532
11-26 0.002532
11-15 0.002532
11-09 0.002532
10-26 0.002532
09-28 0.002532
11-18 0.002532
10-12 0.002532
10-01 0.002532
09-19 0.002532
09-23 0.002532
10-11 0.002532
Name: time, Length: 73, dtype: float64
6,利用画图库,制作折线图和柱状图
7,分析
李子染,在12-10和12-6搜索达到高峰,整个12月开始都是活跃,从11月开始,这与流量推送和媒体大肆爆光相关。