Python|30行代码实现微博热榜爬虫(及可视化进阶)
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
def get_hot_weibo_top10(date):
url = f'https://s.weibo.com/top/summary?cate=realtimehot&date={date}'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
content_list = soup.find_all('td', class_='td-02')
rank_list = soup.find_all('td', class_='td-01')
weibo_list = []
for content in content_list:
weibo_list.append(content.text.strip())
rank_weibo = []
for rank in rank_list:
rank_weibo.append(rank.text.strip())
return rank_weibo, weibo_list
def visualize_weibo_hot(date):
rank, weibo = get_hot_weibo_top10(date)
plt.figure(figsize=(10, 6))
plt.barh(range(len(rank)), range(1, len(rank)+1), align='center')
plt.yticks(range(len(rank)), [f"{v} {k}" for v, k in zip(rank, weibo)])
plt.xlabel('热度')
plt.ylabel('微博')
plt.title(f'微博热榜 TOP 10 - {date}')
plt.savefig(f'weibo_hot_{date}.png', bbox_inches='tight')
# 使用方法
visualize_weibo_hot('2023-03-27')
这段代码实现了微博热榜的爬取和可视化,并展示了如何将爬取的数据保存为图片。这个例子简洁明了,并且使用了requests和BeautifulSoup进行网页解析,pandas和matplotlib进行数据分析和可视化。
评论已关闭