[英]Select and modify xpath nodes after specific text
您可以使用那個translation:
文本節點將作者與翻譯者區分開-作者是“ translation:”文本節點的同級兄弟,譯者是同級兄弟。
作者:
//text()[contains(., 'translation:')]/preceding-sibling::a[@class='booklink' and contains(@href, '/author/')]/text()
譯員:
//text()[contains(., 'translation:')]/following-sibling::a[@class='booklink' and contains(@href, '/author/')]/text()
工作示例代碼:
from lxml.html import fromstring
data = """
<td>
<a class="booklink" href="/author/43710/Author 1">Author 1</a>
,
<a class="booklink" href="/author/46907/Author 2">Author 2</a>
<br>
translation:
<a class="booklink" href="/author/47669/translator 1">Translator 1</a>
,
<a class="booklink" href="/author/9382/translator 2">Translator 2</a>
</td>"""
root = fromstring(data)
authors = root.xpath("//text()[contains(., 'translation:')]/preceding-sibling::a[@class='booklink' and contains(@href, '/author/')]/text()")
translators = root.xpath("//text()[contains(., 'translation:')]/following-sibling::a[@class='booklink' and contains(@href, '/author/')]/text()")
print(authors)
print(translators)
印刷品:
['Author 1', 'Author 2']
['Translator 1', 'Translator 2']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.