Iowa
Iowa

Reputation: 2481

scrapy and xpath: get the text in a child element, if the parent element contains text

How do i get the text of a child element, if the parent element contains text with a specific string?

For example:

<li>
    "string1"
    <span>
        "Hello"
    </span>
</li>
<li>
    "string2"
    <span>
        "Ola"
    </span>
</li>

From the above html code, how to get only string "Ola" using xpath?

Upvotes: 0

Views: 412

Answers (1)

Ralf
Ralf

Reputation: 1813

Without knowing scrapy, I would try

//li[text()[contains(.,"string2")]]/span/text()
  • //li[text()[contains(.,"string2")]] select a li element that text contains string2
  • /span select a element span below the selected li
  • /text(): return the text of the selected span element

Update: This is simpler and should also work:

//li[contains(text(),"string2")]/span/text()

Upvotes: 1

Related Questions