我尝试计算此URL'https://seedly.sg/reviews/p2p-lending/funding-societies'中每个评分列的星级(评分)
我使用硒来自动化整个过程。但是star,star_count和star_count_list中都没有任何内容。该代码对我来说合乎逻辑,并且看起来还不错,我可以知道我的代码有什么问题吗?
谢谢。
##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)
尽管您的代码ID非常正确,但是您尝试定位svg元素的方式不正确。
您需要替换xpath
//svg[contains(@stroke,"57CF8D")]
with
//*[local-name() = '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']")