如何使用selenium和python获取标签中的符号文本?

时间:2022-11-27 16:56:15

I want each user rating from this link:

我想要从这个链接的每个用户评级:

data-iconr="ù" this produce rating 4.0 or anything

data-iconr =“ù”这产生评级4.0或任何东西

you can have look on div tag

你可以看看div标签

<div class="left bold zdhl2 tooltip icon-font-level-7" data-iconr="ù">Rated</div>

Is there any way to get rating of each user?

有没有办法获得每个用户的评级?

Also, how I can get xpath of div tag because it changes the class of each div?

另外,我如何获得div标签的xpath,因为它改变了每个div的类?

1 个解决方案

#1


It might not be bullet-proof, but, you can rely on the class name of the element, it changes with the rating - e.g. for 5.0 there is icon-font-level-9 class, for 4.5 - icon-font-level-8 etc.

它可能不是防弹的,但是,你可以依赖元素的类名,它随着评级而变化 - 例如对于5.0,有icon-font-level-9类,用于4.5 - icon-font-level-8等。

Implementation:

import re
from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.zomato.com/ncr/salad-days-dlf-cyber-city-gurgaon")

mapping = {
    "icon-font-level-9": 5.0,
    "icon-font-level-8": 4.5,
    "icon-font-level-6": 3.5
    # ... TODO
}

pattern = re.compile(r"icon-font-level-\d+")
for review in driver.find_elements_by_css_selector("div[itemprop=review]"):
    author = review.find_element_by_css_selector("div[itemprop=author] div[itemprop=name] a").text

    rating_class = review.find_element_by_xpath(".//div[. = 'Rated']").get_attribute('class')
    rating = mapping.get(pattern.search(rating_class).group(0))
    print author, rating

Prints:

Vandhna Babu 4.5
Mohit Yadav 3.5
Pulkit1283 4.5
Grub Society 5.0
Joel George 5.0

#1


It might not be bullet-proof, but, you can rely on the class name of the element, it changes with the rating - e.g. for 5.0 there is icon-font-level-9 class, for 4.5 - icon-font-level-8 etc.

它可能不是防弹的,但是,你可以依赖元素的类名,它随着评级而变化 - 例如对于5.0,有icon-font-level-9类,用于4.5 - icon-font-level-8等。

Implementation:

import re
from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.zomato.com/ncr/salad-days-dlf-cyber-city-gurgaon")

mapping = {
    "icon-font-level-9": 5.0,
    "icon-font-level-8": 4.5,
    "icon-font-level-6": 3.5
    # ... TODO
}

pattern = re.compile(r"icon-font-level-\d+")
for review in driver.find_elements_by_css_selector("div[itemprop=review]"):
    author = review.find_element_by_css_selector("div[itemprop=author] div[itemprop=name] a").text

    rating_class = review.find_element_by_xpath(".//div[. = 'Rated']").get_attribute('class')
    rating = mapping.get(pattern.search(rating_class).group(0))
    print author, rating

Prints:

Vandhna Babu 4.5
Mohit Yadav 3.5
Pulkit1283 4.5
Grub Society 5.0
Joel George 5.0