简体   繁体   中英

How to extract text from an element that does not have an xpath

I am trying to web scrape the dollar sign rating for each restaurant on a food delivery website, however, there is no available xpath.

<!-- react-text: 2108 -->
"$$"
<!-- /react-text -->

The above code is what is used for the dollar ratings from when I inspected the website. I've tried using the line directly above:

    <i class="icon-bullet--small">·</i>

However, this outputs the period since it is not for the dollar rating. I've also tried using:

    cost = ['//li[{}]/a/div[2]/p[2]/!'.format(x) for x in range(1, 999)]

as well as using "!--" and "react" and "react-text" in the xpath, but none of it works. Any suggestions on how to approach this?

This XPath,

//comment()[normalize-space() = "react-text: 2108"]/following-sibling::text()

will select the text node immediately following the targeted comment, returning

"$$"

as requested.


Important note: @DebanjanB has helpfully pointed out that the comment containing react-text: 2108 is a React directive that Selenium won't see unless the content is extracted as page_source . Thanks, Debanjan!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM