它可能不是防彈的,但是,您可以依靠元素的類名稱,它隨着評級而變化 - 例如,爲5.0
有icon-font-level-9
類,爲4.5
- icon-font-level-8
等
實現:
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
打印:
Vandhna Babu 4.5
Mohit Yadav 3.5
Pulkit1283 4.5
Grub Society 5.0
Joel George 5.0
謝謝你,這是完美的工作。 – Mukesh