Home >Backend Development >Python Tutorial >Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1)

Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1)

Python当打之年
Python当打之年forward
2023-08-10 15:53:101017browse


##This issue isb4f154bc2a2697b29c34edd5ee550e13Series of articlesThe content of the previous article introduces how to use Python to regularly crawl knowledge Hu hot list/Weibo hot search data, andsave it to a CSV file for subsequent visualization. The timing diagram part will be innext articleIntroduced in the content, I hope it will be helpful to you.

涉及到的内容:
pandas — 数据处理
schedule — 定时任务
#json - Data Format

read_html — Web form processing


1. Preparation

1.1 Import module
import json
import time
import requests
import schedule
import pandas as pd
from fake_useragent import UserAgent


##

2. 知乎热榜数据

2.1 网页分析 
知乎热榜电脑端接口:
https://www.zhihu.com/hot
知乎热榜手机端接口:
https://api.zhihu.com/topstory/hot-list?limit=10&reverse_order=0

注意:电脑端端直接F12调试页即可看到热榜数据,手机端需要借助抓包工具查看,这里我们使用手机端接口(返回json格式数据,解析比较方便)。

Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1)

2.2 Get data

##Code:

def getzhihudata(url, headers):
    r = requests.get(url, headers=headers)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    datas = json.loads(r.text)['data']
    allinfo = []
    time_mow = time.strftime("%Y-%m-%d %H:%M", time.localtime())
    print(time_mow)
    for indx,item in enumerate(datas):
        title = item['target']['title']
        heat = item['detail_text'].split(' ')[0]
        answer_count = item['target']['answer_count']
        follower_count = item['target']['follower_count']
        href = item['target']['url']
        info = [time_mow, indx+1, title, heat, answer_count, follower_count, href]
        allinfo.append(info)
    # 仅首次加表头
    global csv_header
    df = pd.DataFrame(allinfo,columns=['时间','排名','标题','热度(万)','回答数','关注数','链接'])
    print(df.head())

定时间隔设置1S:

# 每1分钟执行一次爬取任务:
schedule.every(1).minutes.do(getzhihudata,zhihu_url,headers)
while True:
     schedule.run_pending()
     time.sleep(1)

效果:

Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1)

2.3 保存数据 

df.to_csv('zhuhu_hot_datas.csv', mode='a+', index=False, header=csv_header)
csv_header = False
注意csv_header的设置,涉及到是否写入表头字段。


3. Weibo hot search data

##3.1 Web page analysis

##Weibo hot search URL:

https://s.weibo.com/top/summary

Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1)
F12 View web page source code:
Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1)

##The data is in the f5d188ed2c074f8b944552db028f98a1 tag of the web page.

##3.2 Obtain data

代码:

def getweibodata():
    url = 'https://s.weibo.com/top/summary'
    r = requests.get(url, timeout=10)
    r.encoding = r.apparent_encoding
    df = pd.read_html(r.text)[0]
    df = df.loc[1:,['序号', '关键词']]
    df = df[~df['序号'].isin(['•'])]
    time_mow = time.strftime("%Y-%m-%d %H:%M", time.localtime())
    print(time_mow)
    df['时间'] = [time_mow] * df.shape[0]
    df['排名'] = df['序号'].apply(int)
    df['标题'] = df['关键词'].str.split(' ', expand=True)[0]
    df['热度'] = df['关键词'].str.split(' ', expand=True)[1]
    df = df[['时间','排名','标题','热度']]
    print(df.head())

定时间隔设置1S效果:

Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1)

3.3 保存数据 

df.to_csv('weibo_hot_datas.csv', mode='a+', index=False, header=csv_header)

结果:

Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1)


The above is the detailed content of Crawler + Visualization | Python Zhihu Hot List/Weibo Hot Search Sequence Chart (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Python当打之年. If there is any infringement, please contact admin@php.cn delete