Python 为什么不';这算星代码不管用吗?

Python 为什么不';这算星代码不管用吗?,python,selenium,for-loop,selenium-webdriver,counting,Python,Selenium,For Loop,Selenium Webdriver,Counting,我试图计算此URL“”中每个评级列的星级(评级) 我使用selenium自动化整个过程。 但是星号、星号计数和星号计数列表中都没有任何内容。 代码对我来说是合乎逻辑的,而且似乎很好,我可以知道我的代码中有什么问题吗 提前谢谢 ##These are basic setups from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui

我试图计算此URL“”中每个评级列的星级(评级)

我使用selenium自动化整个过程。 但是星号、星号计数和星号计数列表中都没有任何内容。 代码对我来说是合乎逻辑的,而且似乎很好,我可以知道我的代码中有什么问题吗

提前谢谢

##These are basic setups
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.common.exceptions import TimeoutException
from time import sleep
import pandas as pd

'''Create new instance of Chrome in Incognito mode'''
##Adding the incognito argument to our webdriver
option = webdriver.ChromeOptions()
option.add_argument(" — incognito")
##create a new instance of Chrome
browser = webdriver.Chrome('/Users/w97802/chromedriver')

'''Scrape Basic Info'''
from parsel import Selector
url = 'https://seedly.sg/reviews/p2p-lending/funding-societies'
browser.get(url)
selector = Selector(text=browser.page_source)

####################################################################
##This is the star-count code
'''Count stars simple'''
star_count_list = []

ratingcolumn = browser.find_elements_by_xpath('//div[contains(@class,"qr0ren-7 euifNX")]')
for rows in ratingcolumn:
    star_count = 0
    stars = browser.find_elements_by_xpath('//svg[contains(@stroke,"57CF8D")]')
    for numofstars in range(0,len(stars)):
        star_count += 1
        star_count_list.append(star_count)

print(stars)
print(star_count)
'''Print Stars Result''' 
for i,e in enumerate(star_count_list, start=1):
        print ('\n \n \n ' + str(i) + '. \n', e)  


尽管您编写了代码,但试图定位svg元素的方式是不正确的

您需要替换xpath

//svg[contains(@stroke,"57CF8D")]

我很好奇您是如何填充这样的xpath的。我在chrome上测试过,没有发现任何问题。更好的方法是在chrome中填充一次,以确保安全

作为替代,您可以使用css选择器来定位相同的元素

div[class='qr0ren-7 euifNX'] svg[stroke='#57CF8D']
在您的代码中:

stars = browser.find_elements_by_css_selector("div[class='qr0ren-7 euifNX'] svg[stroke='#57CF8D']")

非常感谢你的帮助。我试图指定路径:browser.find_elements_by_xpath('//div[@class=“qr0ren-7 euifNX”]/span/span/svg[contains(@stroke,“#57CF8D”)),但它不起作用。我不太明白为什么,因为我按照每个下拉菜单指定了路径。svg标记之前的路径规范仍然可以。我可以知道为什么吗?提前感谢。svg主要用于矢量类型图,如条形图、饼图、可缩放图标、可缩放徽标和其他设计图。svg查看器用于渲染元素。通常SVG元素不会被selenium捕获。我明白了!我使用了您提供的代码,结果发现颜色代码并不是区分彩色星和非彩色星的原因。颜色代码相同,但非彩色星的宽度为0%,因此我派生了代码:browser.find_elements_通过xpath('//div[@class=“qr0ren-7 euifNX”]/span/span/span[contains(@style,“width:100%”)但是它说无效的选择器:无法找到带有xpath表达式的元素。我想知道为什么?因为我在每个下拉菜单中都检查了它,它应该是正确的?谢谢!我明白了!当然!非常感谢您的帮助!祝您在新的一年里过得很棒!
stars = browser.find_elements_by_css_selector("div[class='qr0ren-7 euifNX'] svg[stroke='#57CF8D']")