【发布时间】:2015-12-30 18:37:27
【问题描述】:
我必须在 HTML 表格中检索文本,在单元格中,文本有时在 <div> 内,有时不在。
如何使 XPath 中的 div 成为可选?
我的实际代码:
stuff = tree.xpath("/html/body/table/tbody/tr/td[5]/div/text()")
想要的伪代码:
stuff = tree.xpath("/html/body/table/tbody/tr/td[5]/div or nothing/text()")
【问题讨论】:
-
normalize-space(/html/body/table/tbody/tr/td[5])
标签: python html xml xpath web-scraping