【发布时间】:2011-09-02 21:27:16
【问题描述】:
我有以下html代码:
<td class="role" style=""><a href="/wiki/Chairman">Chairman</a> of <a href="/wiki/Microsoft">Microsoft</a><br />
<a href="/wiki/Chairman">Chairman</a> of <a href="/wiki/Corbis">Corbis</a><br />
Co-Chair of the <a href="/wiki/Bill_%26_Melinda_Gates_Foundation">Bill & Melinda Gates Foundation</a><br />
<a href="/wiki/Creative_Director" title="Creative Director" class="mw- redirect">Director</a> of <a href="/wiki/Berkshire_Hathaway">Berkshire Hathaway</a><br/>
<a href="/wiki/CEO" class="mw-redirect" title="CEO">CEO</a> of <a href="/wiki/Cascade_Investment">Cascade Investment</a></td>
对于上面的td元素,语义上有五行,以"<br/>"分隔,我想得到这五行为:
Chairman of Microsoft
Chariman of Borbis
Co-Char of the Bill&Melinda Gates Fundation
Creative Director of Berkshire Hathaway
CEO of Cascade Investment
目前,我的解决方案是首先在这个td 中获取所有br,如:
br_value = td_node.select('.//br')
然后对于每个 br_value,我使用以下代码获取所有文本:
for br_item in br_value:
one_item = br_item.select('.//preceding-sibling::*/text()').extract()
在这种情况下,我可以得到这样的行:
Chairman Microsoft
Chariman Borbis
Bill&Melinda Gates Fundation
Director Berkshire Hathaway
CEO Cascade Investment
和我想要的原文相比,他们基本上漏掉了“of”,还有一些其他的文字。
原因是“preceding-sibling”只返回兄弟标签,而不能返回属于其父标签的“text”,如本例中的“of”。
这里有人知道如何提取由br标签分隔的完整信息吗?
谢谢
【问题讨论】:
-
很高兴投票并接受答案,请
标签: xpath html-parsing