四大解析器(BeautifulSoup、PyQuery、lxml、正则)性能比较
用标题中的四种方式解析网页,比较其解析速度。当然比较结果数值与电脑配置,python版本都有关系,但总体差别不会很大。
下面是我的结果,lxml xpath最快,bs4最慢
==== Python version: 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] ===== ==== Total trials: 10000 =====
bs4 total time: 5.5
pq total time: 0.9
lxml (cssselect) total time: 0.8
lxml (xpath) total time: 0.5
regex total time: 1.1 (doesn't find all p)
以下是测试代码
# -*- coding: utf-8 -*- """
@Datetime: 2019/3/13
@Author: Zhang Yafei
"""
import re
import sys
import time
import requests
from lxml.html import fromstring
from pyquery import PyQuery as pq
from bs4 import BeautifulSoup as bs headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'} def Timer():
a = time.time()
while True:
c = time.time()
yield time.time() - a
a = c # ################# start request #################
timer = Timer()
url = "https://www.python.org/"
html = requests.get(url, headers=headers).text
num = 10000
print('\n==== Python version: %s =====' % sys.version)
print('\n==== Total trials: %s =====' % num)
next(timer) # ################# bs4 #########################
soup = bs(html, 'lxml')
for x in range(num):
paragraphs = soup.findAll('p')
t = next(timer)
print('bs4 total time: %.1f' % t)
# ################ pyquery #######################
d = pq(html)
for x in range(num):
paragraphs = d('p')
t = next(timer)
print('pq total time: %.1f' % t)
# ############### lxml css #########################
tree = fromstring(html)
for x in range(num):
paragraphs = tree.cssselect('p')
t = next(timer)
print('lxml (cssselect) total time: %.1f' % t)
# ############## lxml xpath #######################
tree = fromstring(html)
for x in range(num):
paragraphs = tree.xpath('.//p')
t = next(timer)
print('lxml (xpath) total time: %.1f' % t)
# ############### re ##########################
for x in range(num):
paragraphs = re.findall('<[p ]>.*?</p>', html)
t = next(timer)
print('regex total time: %.1f (doesn\'t find all p)\n' % t)
测试代码二
# -*- coding: utf-8 -*- """
@Datetime: 2019/3/13
@Author: Zhang Yafei
"""
import functools
import re
import sys
import time import requests
from bs4 import BeautifulSoup as bs
from lxml.html import fromstring
from pyquery import PyQuery as pq def timeit(fun):
@functools.wraps(fun)
def wrapper(*args, **kwargs):
start_time = time.time()
res = fun(*args, **kwargs)
print('运行时间为%.6f' % (time.time() - start_time))
return res return wrapper @timeit # time1 = timeit(time)
def time1(n):
return [i * 2 for i in range(n)] # ################# start request #################
url = "https://www.taobao.com/"
html = requests.get(url).text
num = 10000
print('\n==== Python version: %s =====' % sys.version)
print('\n==== Total trials: %s =====' % num) @timeit
def bs4_test():
soup = bs(html, 'lxml')
for x in range(num):
paragraphs = soup.findAll('p')
print('bs4 total time:') @timeit
def pq_test():
d = pq(html)
for x in range(num):
paragraphs = d('p')
print('pq total time:') @timeit
def lxml_css():
tree = fromstring(html)
for x in range(num):
paragraphs = tree.cssselect('p')
print('lxml (cssselect) total time:') @timeit
def lxml_xpath():
tree = fromstring(html)
for x in range(num):
paragraphs = tree.xpath('.//p')
print('lxml (xpath) total time:') @timeit
def re_test():
for x in range(num):
paragraphs = re.findall('<[p ]>.*?</p>', html)
print('regex total time:') if __name__ == '__main__':
bs4_test()
pq_test()
lxml_css()
lxml_xpath()
re_test()
测试结果
==== Python version: 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] ===== ==== Total trials: 10000 =====
bs4 total time:
运行时间为9.049424
pq total time:
运行时间为0.899639
lxml (cssselect) total time:
运行时间为0.841596
lxml (xpath) total time:
运行时间为0.619440
regex total time:
运行时间为1.207861
四大解析器(BeautifulSoup、PyQuery、lxml、正则)性能比较的更多相关文章
- Python HTML解析器BeautifulSoup(爬虫解析器)
BeautifulSoup简介 我们知道,Python拥有出色的内置HTML解析器模块——HTMLParser,然而还有一个功能更为强大的HTML或XML解析工具——BeautifulSoup(美味的 ...
- 转:Python网页解析:BeautifulSoup vs lxml.html
转自:http://www.cnblogs.com/rzhang/archive/2011/12/29/python-html-parsing.html Python里常用的网页解析库有Beautif ...
- 正则表达式、BeautifulSoup、Lxml进行性能对比
爬取方法 性能 使用难度 安装难度 正则表达式 快 困难 简单(内置) BeautifulSoup 慢 简单 简单 Lxml 快 简单 相对困难
- HTML解析器BeautifulSoup
BeautifulSoup是Python的一个库,可解析用urllib2抓取下来的HTML 1.Beautiful Soup 安装 可以利用 pip 来安装,在Python程序中导入 pip inst ...
- 爬虫----爬虫解析库Beautifulsoup模块
一:介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
- 爬虫解析库——BeautifulSoup
解析库就是在爬虫时自己制定一个规则,帮助我们抓取想要的内容时用的.常用的解析库有re模块的正则.beautifulsoup.pyquery等等.正则完全可以帮我们匹配到我们想要住区的内容,但正则比较麻 ...
- 爬虫解析库BeautifulSoup的一些笔记
BeautifulSoup类使用 基本元素 说明 Tag 标签,最基本的信息组织单元,分别是<>和</>标明开头和结尾 Name 标签的名字,<p></p ...
- 爬虫解析库beautifulsoup
一.介绍 Beautiful Soup是一个可以从HTML或XML文件中提取数据的python库. #安装Beautiful Soup pip install beautifulsoup4 #安装解析 ...
- Beautiful Soup常见的解析器
Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快 ...
随机推荐
- 微信小程序测试方法总结
最近的新项目是小程序加web端后台管理 主要找了些文章方便自己使用也分享给大家: 小程序官方文档 https://developers.weixin.qq.com/miniprogram/design ...
- LeetCode算法题-Non-decreasing Array(Java实现)
这是悦乐书的第283次更新,第300篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第151题(顺位题号是665).给定一个包含n个整数的数组,您的任务是通过修改最多1个元 ...
- 【Python 21】52周存钱挑战1.0
1.案例描述 按照52周存钱法,存钱人必须在一年52周内,每周递存10元.例如,第一周存10元,第二周存20元,第三周存30元,直到第52周存520元. 记录52周后能存多少钱?即10+20+30+. ...
- styled components草根中文版文档
1.styled components官网网址 https://www.styled-components.com/docs 以组件的形式来写样式. 1.1安装 yarn add styled-c ...
- 文本分类实战(八)—— Transformer模型
1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...
- shell杀死指定端口的进程
杀死端口代码如下: lsof -i: kill - PID 上面的与下面的代码作用相同. 命令如下所示(这种方式更自动化): kill - $(netstat -nlp | grep : | awk ...
- 前端——JavaScript
何谓JavaScript?它与Java有什么关系? JavaScript与HTML.CSS组合使用应用于前端开发,JavaScript是一门独立的语言,浏览器内置了JS的解释器.它除了和Java名字长 ...
- 在C#/.NET应用程序开发中创建一个基于Topshelf的应用程序守护进程(服务)
本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...
- Error:Execution failed for task ':app:processDebugManifest'. Manifest merger failed with multiple errors, see logs
这个异常在网上一搜会出现很多答案,也可能都对. 我都尝试过但是不符合我这边的要求,问题得不到解决.网上的说法是对的,jar包冲突.不过究竟是哪里冲突没办法判断. 最后尝试了一下在module的中没用的 ...
- asp.net webapi中helppage
今天研究了下webapi,发现还有自动生成接口说明文档提供测试的功能 参考:https://docs.microsoft.com/en-us/aspnet/web-api/overview/getti ...