【python】爬取百度热搜排行榜Top50+可视化
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import numpy as np
# 请求URL,获取网页内容
def get_html(url):
try:
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.3'}
response = requests.get(url, headers=headers)
return response.text
except requests.RequestException as e:
print(e)
return None
# 解析网页,提取热搜词
def parse_html(html):
soup = BeautifulSoup(html, 'lxml')
hot_list = soup.find_all('div', class_='title-content')
hot_searches = [item.text.strip() for item in hot_list]
return hot_searches
# 数据可视化
def visualize(data):
plt.figure(figsize=(10, 8))
plt.barh(range(len(data)), np.arange(1, len(data)+1), align='center', color='orange')
plt.yticks(np.arange(0.5, len(data), 1), data)
plt.xlabel('热度指数')
plt.ylabel('搜索词')
plt.title('百度热搜排行榜')
plt.show()
# 主函数
def main():
url = 'http://top.baidu.com/buzz?b=1&c=1&class=3&fr=www_v2'
html = get_html(url)
data = parse_html(html)
visualize(data)
if __name__ == '__main__':
main()
这段代码实现了从百度热搜排行榜获取数据,并使用matplotlib进行数据可视化的功能。代码简洁,注重逻辑性和实用性,是学习爬虫和数据可视化的好例子。
评论已关闭