使用lxml.html,如何在不使用for循環的情況下訪問單個元素?在lxml.html中訪問輸出的第一個元素
這是HTML:
<tr class="headlineRow">
<td>
<span class="headline">This is some awesome text</span>
</td>
</tr>
例如,這將失敗,並IndexError:
for row in doc.cssselect('tr.headlineRow'):
headline = row.cssselect('td span.headline')
print headline[0]
這將傳遞:
for row in doc.cssselect('tr.headlineRow'):
headline = row.cssselect('td span.headline')
for first_thing in headline:
print headline[0].text_content()