简体   繁体   中英

Incredibly basic lxml questions: getting HTML/string content of lxml.etree._Element?

This is such a basic question that I actually can't find it in the docs :-/

In the following:

img = house_tree.xpath('//img[@id="mainphoto"]')[0]

How do I get the HTML of the <img/> tag?

I've tried adding html_content() but get AttributeError: 'lxml.etree._Element' object has no attribute 'html_content' .

Also, it was a tag with some content inside (eg <p>text</p> ) how would I get the content (eg text )?

Many thanks!

I suppose it will be as simple as:

from lxml.etree import tostring
inner_html = tostring(img)

As for getting content from inside <p> , say, some selected element el :

content = el.text_content()

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