2016-12-28 9 views

答えて

2

あなたが翻訳者からの著者を区別するために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) 

プリント:

​​
関連する問題