在当今的互联网时代,社交媒体平台如同一个巨大的脉搏传感器,实时反映着社会的关注焦点和舆论动向。新浪微博,作为中国领先的社交媒体平台之一,其热搜榜更是成为了解当下热门话题和流行趋势的黄金入口。对于市场研究人员、数据分析师或是任何希望从宏观角度洞察公众情绪的从业者而言,能够自动化地获取这些数据,无疑具有极高的价值。
本文将深入浅出地介绍如何利用Python生态中两个极其强大且易用的库——Requests和BeautifulSoup,来构建一个轻量级却高效的微博热搜榜及话题内容爬虫。我们将从原理分析到代码实现,一步步揭开微博爬虫的神秘面纱。
在开始之前,我们首先要理解为什么选择这两个库:
它们的组合是处理静态网页内容的经典“王道组合”,非常适合微博热搜榜这种内容相对结构化且直接渲染在HTML中的页面。
环境准备:
确保你的Python环境(建议3.6以上)中已经安装了以下库。如果没有,可以通过pip
命令安装
爬虫的核心工作流程可以概括为:模拟请求 -> 获取数据 -> 解析数据 -> 存储数据。
然而,爬取现代网站,尤其是大型网站 like Weibo,绝非一帆风顺。主要难点在于:
我们的应对策略是:尽可能地模拟一个真实浏览器的请求,通过定制请求头(Headers)来绕过初步的反爬虫检查。
接下来,我们将一步步实现爬虫的每个环节。
import requests
from bs4 import BeautifulSoup
import time
# 目标URL - 微博热搜榜页面
url = 'https://s.weibo.com/top/summary'
这是绕过反爬虫最关键的一步。我们需要在请求中携带User-Agent
、Cookie
等关键信息。
https://s.weibo.com/top/summary
)。F12
打开开发者工具。Network
(网络)标签页,刷新页面。Name
栏下找到第一个文档(通常是top/summary
或summary
),点击它。Headers
(请求头),向下滑动找到Request Headers
(请求头)部分。User-Agent
和Cookie
的值复制出来。重要提示:Cookie包含了你的登录身份信息,请勿泄露! 这里的示例代码需要你填入自己获取到的值。
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Cookie': '你的Cookie值', # 请替换为你自己从浏览器获取的有效Cookie
'Referer': 'https://s.weibo.com/',
}
使用Requests库发送GET请求,并检查请求是否成功。
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # 如果状态码不是200,则抛出异常
response.encoding = response.apparent_encoding # 自动判断编码,避免乱码
print("请求成功!")
except requests.exceptions.RequestException as e:
print(f"请求失败:{e}")
exit()
这是数据抓取的核心。我们需要分析热搜榜页面的HTML结构,找到包含热搜条目的标签。
Elements
(元素)标签页的检查工具(箭头图标),点击页面上的一个热搜标题,定位到对应的HTML代码。你会发现每个热搜条目都在一个<tr>
标签内。<tr>
标签,然后遍历这些标签,从中提取排名、标题、搜索量、标签等信息。import requests
from bs4 import BeautifulSoup
import time
# 代理服务器信息
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
# 构造代理字典
proxyMeta = f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
proxies = {
"http": proxyMeta,
"https": proxyMeta,
}
# 目标URL - 微博热搜榜页面
url = 'https://s.weibo.com/top/summary'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Cookie': '你的Cookie值', # 请替换为你自己从浏览器获取的有效Cookie
'Referer': 'https://s.weibo.com/',
}
try:
# 在requests.get()中添加proxies参数
response = requests.get(url, headers=headers, proxies=proxies, timeout=10)
response.raise_for_status() # 如果状态码不是200,则抛出异常
response.encoding = response.apparent_encoding # 自动判断编码,避免乱码
print("请求成功!")
except requests.exceptions.RequestException as e:
print(f"请求失败:{e}")
exit()
soup = BeautifulSoup(response.text, 'html.parser')
# 通过检查元素,发现热搜列表在一个id为`pl_top_realtimehot`的table下的tbody中
# 我们首先找到这个table
hot_search_table = soup.find('table', attrs={'id': 'pl_top_realtimehot'})
if hot_search_table is None:
print("未找到热搜列表表格,请检查页面结构或Cookie是否有效")
exit()
# 然后找到表格内的所有tr标签
items = hot_search_table.find_all('tr')
hot_searches = [] # 用于存储所有热搜字典的列表
# 遍历每一个tr标签(跳过第一个表头tr)
for index, tr in enumerate(items[1:], start=1): # 从1开始计数,作为排名
# 提取热搜标题,通常在<td class="td-02">下的<a>标签里
title_tag = tr.find('td', class_='td-02').find('a')
if not title_tag:
continue
title = title_tag.get_text(strip=True)
# 提取链接
link = "https://s.weibo.com" + title_tag['href'] if title_tag.get('href') else None
# 提取搜索量,可能在<span>标签里
span_tag = tr.find('td', class_='td-02').find('span')
hot_count = span_tag.get_text(strip=True) if span_tag else "未知"
# 提取标签,比如`新`、`热`、`爆`,可能在<a>标签下的<i>标签里
i_tag = title_tag.find('i')
tag = i_tag.get_text(strip=True) if i_tag else ""
# 构建一个字典来存储一条热搜信息
hot_search_item = {
'rank': index,
'title': title,
'url': link,
'hot_count': hot_count,
'tag': tag
}
hot_searches.append(hot_search_item)
# 打印单条结果(可选)
print(f"{index}. {title} [{tag}] (热度: {hot_count}) - {link}")
# 提示完成
print(f"\n总共爬取到 {len(hot_searches)} 条热搜。")
# 获取当前时间作为文件名
current_time = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
filename_txt = f'weibo_hot_searches_{current_time}.txt'
with open(filename_txt, 'w', encoding='utf-8') as f:
f.write(f"微博热搜榜抓取时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}\n")
f.write("="*50 + "\n")
for item in hot_searches:
f.write(f"{item['rank']:>2}. {item['title']} [{item['tag']}] (热度: {item['hot_count']})\n")
f.write(f" 链接:{item['url']}\n")
print(f"数据已保存到 {filename_txt}")
将提取的数据存储下来以供后续分析是最终目的。这里我们演示如何保存为TXT和CSV格式。
保存为TXT文件:
python
# 获取当前时间作为文件名
current_time = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
filename_txt = f'weibo_hot_searches_{current_time}.txt'
with open(filename_txt, 'w', encoding='utf-8') as f:
f.write(f"微博热搜榜抓取时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}\n")
f.write("="*50 + "\n")
for item in hot_searches:
f.write(f"{item['rank']:>2}. {item['title']} [{item['tag']}] (热度: {item['hot_count']})\n")
f.write(f" 链接:{item['url']}\n")
print(f"数据已保存到 {filename_txt}")
保存为CSV文件(更利于数据分析):
import csv
filename_csv = f'weibo_hot_searches_{current_time}.csv'
with open(filename_csv, 'w', newline='', encoding='utf-8-sig') as f: # 'utf-8-sig'防止Excel打开中文乱码
fieldnames = ['排名', '标题', '标签', '热度', '链接']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for item in hot_searches:
writer.writerow({
'排名': item['rank'],
'标题': item['title'],
'标签': item['tag'],
'热度': item['hot_count'],
'链接': item['url']
})
print(f"数据已保存到 {filename_csv}")
time.sleep(2)
);② 使用代理IP池。find
和find_all
的参数。hot_searches
中的每个url
,用类似的逻辑发送请求和解析。但请注意,话题页面的反爬虫可能更严格,且内容可能是动态加载的。通过本文,我们成功地使用Python的Requests和BeautifulSoup库构建了一个基础的微博热搜榜爬虫。这个过程不仅演示了这两个库的基本用法,更展示了爬虫开发中“分析-模拟-提取”的核心思想。
虽然这个爬虫是基础版的,但它构成了一个强大的起点。你可以在此基础上扩展功能,例如添加定时任务、接入数据库、进行情感分析、构建可视化大屏等,从而打造出一个属于自己的舆情监控系统。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。