我正在寻找一个用于 Python 的 HTML Parser 模块,它可以帮助我以 Python 列表/字典/对象的形式获取标签。
如果我有以下表格的文件:
<html>
<head>Heading</head>
<body attr1='val1'>
<div class='container'>
<div id='class'>Something here</div>
<div>Something else</div>
</div>
</body>
</html>
那么它应该给我一种通过 HTML 标记的名称或 id 访问嵌套标记的方法,以便我基本上可以要求它获取 div
标记中的内容/文本,其中 class='container'
包含在 {3 } 标签,或类似的东西。
如果您使用过 Firefox 的“检查元素”功能(查看 HTML),您会知道它以一种像树一样的嵌套方式为您提供所有标签。
我更喜欢内置模块,但这可能要求太多了。
我在 Stack Overflow 和互联网上的一些博客上遇到了很多问题,其中大多数都建议 BeautifulSoup 或 lxml 或 HTMLParser,但其中很少有详细说明功能,并且只是作为关于哪个更快/更有效的争论而结束。
这样我就可以要求它获取 div 标记中的内容/文本,其中 class='container' 包含在 body 标记中,或者类似的东西。
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
from bs4 import BeautifulSoup
html = #the HTML code you've written above
parsed_html = BeautifulSoup(html)
print(parsed_html.body.find('div', attrs={'class':'container'}).text)
我猜你不需要性能描述 - 只需阅读 BeautifulSoup 的工作原理。查看它的 official documentation。
我猜你要找的是pyquery:
pyquery:一个类似 jquery 的 python 库。
你想要的一个例子可能是这样的:
from pyquery import PyQuery
html = # Your HTML CODE
pq = PyQuery(html)
tag = pq('div#id') # or tag = pq('div.class')
print tag.text()
它使用与 Firefox 或 Chrome 的检查元素相同的选择器。例如:
https://i.stack.imgur.com/mc3E2.png
检查的元素选择器是“div#mw-head.noprint”。所以在 pyquery 中,你只需要传递这个选择器:
pq('div#mw-head.noprint')
在这里,您可以阅读更多关于 Python 中不同的 HTML 解析器及其性能的信息。尽管这篇文章有点过时,但它仍然为您提供了一个很好的概述。
Python HTML parser performance
我推荐 BeautifulSoup,即使它不是内置的。只是因为它很容易处理这些类型的任务。例如:
import urllib2
from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen('http://www.google.com/')
soup = BeautifulSoup(page)
x = soup.body.find('div', attrs={'class' : 'container'}).text
from bs4 import BeautifulSoup
与其他解析器库相比,lxml
非常快:
http://blog.dispatched.ch/2010/08/16/beautifulsoup-vs-lxml-performance/
http://www.ianbicking.org/blog/2008/03/python-html-parser-performance.html
使用 cssselect
,它也很容易用于抓取 HTML 页面:
from lxml.html import parse
doc = parse('http://www.google.com').getroot()
for div in doc.cssselect('a'):
print '%s: %s' % (div.text_content(), div.get('href'))
import requests
,将缓冲区保存到文件:stackoverflow.com/a/14114741/1518921(或 urllib),使用解析加载保存的文件后,doc = parse('localfile.html').getroot()
1.7
秒,但改为应用 lxml 后,速度提高了近 *100
倍!如果关心性能,lxml 是最好的选择
我推荐使用 lxml 来解析 HTML。请参阅"Parsing HTML" (on the lxml site).
以我的经验,Beautiful Soup 搞砸了一些复杂的 HTML。我相信那是因为 Beautiful Soup 不是解析器,而是一个非常好的字符串分析器。
我建议使用 justext 库:
https://github.com/miso-belica/jusText
用法:Python2:
import requests
import justext
response = requests.get("http://planet.python.org/")
paragraphs = justext.justext(response.content, justext.get_stoplist("English"))
for paragraph in paragraphs:
print paragraph.text
Python3:
import requests
import justext
response = requests.get("http://bbc.com/")
paragraphs = justext.justext(response.content, justext.get_stoplist("English"))
for paragraph in paragraphs:
print (paragraph.text)
我会使用 EHP
这里是:
from ehp import *
doc = '''<html>
<head>Heading</head>
<body attr1='val1'>
<div class='container'>
<div id='class'>Something here</div>
<div>Something else</div>
</div>
</body>
</html>
'''
html = Html()
dom = html.feed(doc)
for ind in dom.find('div', ('class', 'container')):
print ind.text()
输出:
Something here
Something else
不定期副业成功案例分享
lxml
库(请参阅下面的答案)。使用cssselect
,它也非常有用,性能通常比其他可用库好 10 到 100 倍。parsed_html = BeautifulSoup(html)
不适合我,parsed_html = BeautifulSoup(html, 'html.parser')
适合import bs
所以当他们用 bs5 改变主意时,他们不会再次破坏每个人的代码