lxml/Python : get previous-sibling

眉间皱痕 提交于 2019-12-05 14:27:46

This worked:

header = ingred_set.getprevious().xpath(".//text()")[0]

For HTML use BeautifulSoup. It gives you access to previous and next siblings directly:

sibling_soup.b.next_sibling
# <c>text2</c>

sibling_soup.c.previous_sibling
# <b>text1</b>

Also, you can tell BS to use lxml parser in constructor. From practice I can tell that lxml performs better than the default html.parser on ill-formatted input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!