摘要
本文对比了 Parsel 与 BeautifulSoup 两种常用 Python HTML 解析库在性能、用法、易用性和生态上的差异。通过在 eastmoney.com 站点的实战案例,分别用两者实现财经新闻及数据的爬取,演示如何配置爬虫代理 IP以及对抓取结果的分类存储。全文分为四大模块:
- 核心主题:解析库选型要点
- 多分支技术路线:Parsel 与 BeautifulSoup 用法与性能对比
- 图谱展示:思维导图一览
- 路线建议:基于项目需求的选型指引
核心主题
- 项目背景:在爬取 eastmoney.com 时,需要稳定、快速地提取财经新闻列表、文章标题、发布时间、主要数据(如股价、涨跌幅等)
- 选型痛点:
- 性能:解析速度 vs 可维护性
- 用法:CSS/XPath 语法支持 vs API 简洁度
- 生态:社区活跃度、扩展插件支持
多分支技术路线
1. Parsel 路线
Parsel 基于 lxml,支持 XPath 与 CSS Selector,适合对性能要求较高且习惯使用 XPath 的场景。
import requests
from parsel import Selector
# == 代理 IP 配置(亿牛云爬虫代理示例 www.16yun.cn) ==
proxy_host = "proxy.16yun.cn"
proxy_port = "12345"
proxy_user = "16YUN"
proxy_pass = "16IP"
proxy_template = f"http://{proxy_user}:{proxy_pass}@{proxy_host}:{proxy_port}"
proxies = {
"http": proxy_template,
"https": proxy_template,
}
# == 请求头和 Cookie 设置 ==
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"Accept-Language": "zh-CN,zh;q=0.9",
}
cookies = {
"device_id": "xxxxxxxxxxxx",
"other_cookie": "value"
}
def fetch_with_parsel(url):
"""
使用 Parsel 结合 requests 进行页面抓取与解析
"""
resp = requests.get(url, headers=headers, cookies=cookies,
proxies=proxies, timeout=10)
resp.encoding = resp.apparent_encoding
sel = Selector(resp.text)
# 抓取新闻条目列表
items = sel.xpath('//div[@id="quote_right"]/div[contains(@class,"newsList")]/ul/li')
results = []
for li in items:
title = li.xpath('.//a/text()').get()
link = li.xpath('.//a/@href').get()
time = li.xpath('.//span/text()').get()
results.append({
"title": title, "url": link, "time": time})
return results
if __name__ == "__main__":
url = "https://www.eastmoney.com/"
news = fetch_with_parsel(url)
# 简单分类存储:按今日 / 非今日分组
import datetime
today = datetime.datetime.now().strftime("%m-%d")
grouped = {
"today": [], "others": []}
for n in news:
if today in n["time"]:
grouped["today"].append(n)
else:
grouped["others"].append(n)
print("今日财经新闻:", grouped["today"])
2. BeautifulSoup 路线
BeautifulSoup API 简洁,支持多种解析器,社区活跃,适合快速开发和维护。
import requests
from bs4 import BeautifulSoup
# == 代理 IP 配置(同上) ==
proxies = {
"http": proxy_template,
"https": proxy_template,
}
# == 请求头和 Cookie 设置(同上) ==
headers = headers
cookies = cookies
def fetch_with_bs4(url):
"""
使用 BeautifulSoup 结合 requests 进行页面抓取与解析
"""
resp = requests.get(url, headers=headers, cookies=cookies,
proxies=proxies, timeout=10)
resp.encoding = resp.apparent_encoding
soup = BeautifulSoup(resp.text, 'lxml')
# 抓取新闻条目列表
ul = soup.select_one('div#quote_right div.newsList ul')
results = []
for li in ul.find_all('li'):
a = li.find('a')
span = li.find('span')
results.append({
"title": a.get_text(strip=True),
"url": a['href'],
"time": span.get_text(strip=True)
})
return results
if __name__ == "__main__":
url = "https://www.eastmoney.com/"
news = fetch_with_bs4(url)
# 同样的分类存储逻辑
import datetime
today = datetime.datetime.now().strftime("%m-%d")
grouped = {
"today": [], "others": []}
for n in news:
(grouped["today"] if today in n["time"] else grouped["others"]).append(n)
print("今日财经新闻:", grouped["today"])
性能对比
项目 | Parsel(lxml) | BeautifulSoup(lxml) |
---|---|---|
解析速度 | 更快 | 略慢 |
语法灵活性 | XPath + CSS | CSS Selector |
上手难度 | 中等(需 XPath 知识) | 低(API 直观) |
社区及文档 | 较少 | 丰富 |
图谱展示
┌─────────────┐
│ 核心主题 │
│ Parsel vs BS│
└────┬────────┘
│
┌───────────────────────┴───────────────┐
│ │
┌────┴──────┐ ┌──────┴──────┐
│ Parsel │ │BeautifulSoup│
│ 路线 │ │ 路线 │
└───┬───────┘ └──── ─┬──────┘
│ │
─────┴─────┐ ┌────┴────┐
│ 性能高 │ │ API 简洁 │
└───────────┘ └─────────┘
│ │
┌─────┴─────┐ ┌────┴─────┐
│ XPath/CSS │ │CSS Selector│
└───────────┘ └──────────┘
路线建议
- 高性能、大规模抓取:选用 Parsel。利用 XPath 精准定位,配合 lxml 引擎,速度更优。
- 快速原型、易维护:选用 BeautifulSoup。API 简洁、社区成熟,适合团队协作项目。
- 混合使用:在同一项目中,针对简单列表页用 BS4,针对复杂嵌套与深度解析用 Parsel。
- 扩展方向:
- 引入 Scrapy 框架,将 Parsel/BS4 结合 pipelines,实现分布式抓取与数据持久化
- 增加 Selenium/Playwright 支持,处理 JS 渲染页面
通过以上全方位对比和实战演示,相信您能根据项目需求,在 Parsel 和 BeautifulSoup 之间做出最适合的选型。