简体   繁体   中英

Parse local HTML python (lxml)

I'm trying to parse a local HTML with lxml, but I'm getting an error, but I don't know why (sorry in advance for the bad code, I'm new to this).

from lxml import etree, html
from StringIO import StringIO

parser = etree.HTMLParser()
doc = etree.parse(StringIO("test1.html"), parser)
tree = html.fromstring(doc)
CCE = tree.xpath('//div[@data-reactid]/div[@class="browse-summary"]/h1')
URL = tree.xpath('//a[@class="rc-OfferingCard"]/@href')

print 'CCE:', CCE
print 'URL:', URL

And here's the error:

  File "test.py", line 8, in <module>
tree = html.fromstring(doc)
File "/usr/lib/python2.7/dist-packages/lxml/html/__init__.py", line 703, in fromstring
is_full_html = _looks_like_full_html_unicode(html)
TypeError: expected string or buffer

I think you need

tree = etree.parse("text1.html", parser)

without StringIO and fromstring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM