简体   繁体   English

使用 selenium 仅抓取文本

[英]Scraping only text using selenium

I'm trying to scrape the text (dynamic) using selenium which is present as mentioned below in website.我正在尝试使用 selenium 来抓取文本(动态),该文本如下所述在网站中出现。 I searched all over the internet but I got how to scrape a text when it is present in separate div/p/span or any other tag only.我在整个互联网上进行了搜索,但是当文本出现在单独的 div/p/span 或任何其他标签中时,我得到了如何抓取文本。 Please help if you have any idea.如果您有任何想法,请提供帮助。

<div class='css-87uc0g e1tk4kwz1'>
    Text
    <span>
        4.5 
        <span> ::before </span>
    </span>
<div>

I want to extract only text here but that span tag is not allowing it.我想在这里只提取文本,但跨度标签不允许它。

Note: The value of text is dynamic.注意:文本的值是动态的。

Find the div element using following xpath and use javascript executor and get the first child value.使用以下xpath找到div元素并使用javascript executor并获取第一个子值。

element=driver.find_element_by_xpath("//div[.//span[contains(.,'Rating')]]")
print(driver.execute_script('return arguments[0].firstChild.textContent;', element).strip()) 

Or you can simply replace the value.或者您可以简单地替换该值。

element=driver.find_element_by_xpath("//div[.//span[contains(.,'Rating')]]")
print(element.text.replace(driver.find_element_by_xpath("//span[contains(.,'Rating')]").text,'').strip())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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