how to make a xpath-search in two stages?
-
21-12-2019 - |
Question
please help fix sript.
import lxml.html
import requests
doc = lxml.html.document_fromstring("""<html>
<body>
<div class="wrap">
<span class="one">one</span>
<span class="two">two</span>
</div>
</body>
</html>
""")
#first search
result1 = doc.xpath('//div[@class="wrap"]')
print(result1)
#second search
result2 = result1.xpath('//span/text()')
print(result2)
I need the script to search for text in two stages:
- Step 1: Search tree 'div.wrap',
- Step 2: Search for text elements.
The result should be a list of ['one', 'two']
Solution
Join two xpath expressions into one:
>>> doc.xpath('//div[@class="wrap"]/span/text()')
['one', 'two']
If you need to do it in two stages:
>>> result1 = doc.xpath('//div[@class="wrap"]')
>>> result1
[<Element div at 0x2cf5ba0>]
>>> result1[0].xpath('.//span/text()')
['one', 'two']
If result1
can be multiple item list, you need some kind of loop:
>>> [div.xpath('.//span/text()') for div in result1]
[['one', 'two']]
>>> [txt for div in result1 for txt in div.xpath('.//span/text()')]
['one', 'two']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow