【发布时间】:2018-01-20 21:50:03
【问题描述】:
我有两个来自 web 服务的搜索结果,保存为 html,我必须使用 BeautifulSoup 进行解析才能提取一些数据。我注意到其中一个需要大约。比另一个长 35 倍。
有人对此有解释/知道我可以做些什么来提高较慢的 html 文件的性能?
设置:
Python 2.7.13
Jupyter Notebook 4.3.1
beautifulsoup4 (4.5.3)
lxml (3.8.0)
代码:
from bs4 import BeautifulSoup
path = "path to the files"
file_1 = "slow.html"
file_2 = "fast.html"
with open(path+file_1) as rfile_1:
html_1 = rfile_1.read()
with open(path+file_2) as rfile_2:
html_2 = rfile_2.read()
%timeit soup = BeautifulSoup(html_1, 'lxml')
>> 1 loop, best of 3: 4.67 s per loop
%timeit soup = BeautifulSoup(html_2, 'lxml')
>> 10 loops, best of 3: 136 ms per loop
【问题讨论】:
标签: python python-2.7 beautifulsoup