Python 选择并修改特定文本后的xpath节点
我使用此代码获取所有名称:Python 选择并修改特定文本后的xpath节点,python,xpath,lxml,lxml.html,calibre,Python,Xpath,Lxml,Lxml.html,Calibre,我使用此代码获取所有名称: def parse_authors(self, root): author_nodes = root.xpath('//a[@class="booklink"][contains(@href,"/author/")]/text()') if author_nodes: return [unicode(author) for author in author_nodes] 但我想知道,如果有翻译人员在他们的名字旁边加上“(翻译)”:
def parse_authors(self, root):
author_nodes = root.xpath('//a[@class="booklink"][contains(@href,"/author/")]/text()')
if author_nodes:
return [unicode(author) for author in author_nodes]
但我想知道,如果有翻译人员在他们的名字旁边加上“(翻译)”:
example:translator1(translation)
您可以使用
translation:
text节点来区分作者和翻译人员-作者是“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()
翻译人员:
//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)