我试图使用BS4和lxml,所以不是解析html页面两次,有没有办法在lxml中使用汤对象,反之亦然?
self.soup = BeautifulSoup(open(path), "html.parser")
我尝试使用像这样的lxml这个对象
doc = html.fromstring(self.soup)
这是投掷错误TypeError: expected string or bytes-like object
无论如何都有这种用法吗?
我不认为没有经过字符串对象的方法。
from bs4 import BeautifulSoup
import lxml.html
html = """
<html><body>
<div>
<p>test</p>
</div>
</body></html>
"""
soup = BeautifulSoup(html, 'html.parser')
# Soup to lxml.html
doc = lxml.html.fromstring(soup.prettify())
print (type(doc))
print (lxml.html.tostring(doc))
#lxml.html to soup
soup = BeautifulSoup(lxml.html.tostring(doc), 'html.parser')
print (type(soup))
print (soup.prettify())
输出:
<class 'lxml.html.HtmlElement'>
b'<html>\n <body>\n <div>\n <p>\n test\n </p>\n </div>\n </body>\n</html>'
<class 'bs4.BeautifulSoup'>
<html>
<body>
<div>
<p>
test
</p>
</div>
</body>
</html>
更新以回应评论:
您可以使用lxml.etree遍历doc对象:
# Soup to lxml.etree
doc = etree.fromstring(soup.prettify())
it = doc.getiterator()
for element in it:
print("%s - %s" % (element.tag, element.text.strip()))