【发布时间】:2019-02-22 20:08:17
【问题描述】:
我正在使用 Django 和 Python 3.7 。我想加快我的 HTML 解析速度。目前,我正在我的文档中寻找三种类型的元素,就像这样
req = urllib2.Request(fullurl, headers=settings.HDR)
html = urllib2.urlopen(req).read()
comments_soup = BeautifulSoup(html, features="html.parser")
score_elts = comments_soup.findAll("div", {"class": "score"})
comments_elts = comments_soup.findAll("a", attrs={'class': 'comments'})
bad_elts = comments_soup.findAll("span", text=re.compile("low score"))
我读到 SoupStrainer 是提高性能的一种方法 - https://www.crummy.com/software/BeautifulSoup/bs4/doc/#parsing-only-part-of-a-document。但是,所有示例都只讨论了使用单个过滤器解析 HTML 文档。就我而言,我有三个。如何将三个过滤器传递到我的解析中,或者这实际上会产生比我现在这样做的方式更差的性能?
【问题讨论】:
标签: django python-3.x performance parsing beautifulsoup