Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何限制硒的范围;通过“xpath”查找“U元素”;每行的星星数?_Python_Selenium_For Loop_Selenium Webdriver - Fatal编程技术网

Python 我如何限制硒的范围;通过“xpath”查找“U元素”;每行的星星数?

Python 我如何限制硒的范围;通过“xpath”查找“U元素”;每行的星星数?,python,selenium,for-loop,selenium-webdriver,Python,Selenium,For Loop,Selenium Webdriver,我试图通过使用selenium自动化整个过程来计算此url“”中每个评级列的星级(评级) 对于星星计数部分,有5行星星评级。 我尝试使用for循环来限制列表中存储的每个star_计数的范围,从而获得每个用户给出的star评级。然而,这种方法似乎不像列表中存储的那样有效: 1.22 2.22 3.22 4.22 5.二十二 这意味着我的for循环不能限制计数范围。请问有没有其他方法可以限制点票范围以达到我的目的 提前谢谢 ##These are basic setups from selenium

我试图通过使用selenium自动化整个过程来计算此url“”中每个评级列的星级(评级)

对于星星计数部分,有5行星星评级。 我尝试使用for循环来限制列表中存储的每个star_计数的范围,从而获得每个用户给出的star评级。然而,这种方法似乎不像列表中存储的那样有效: 1.22 2.22 3.22 4.22 5.二十二

这意味着我的for循环不能限制计数范围。请问有没有其他方法可以限制点票范围以达到我的目的

提前谢谢

##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 row in ratingcolumn:
    star_count = 0
    stars = browser.find_elements_by_xpath('//div[@class="qr0ren-7 euifNX"]/span/span/span[contains(@style,"width:100%")]')
    for targets in stars:
        star_count += 1
    star_count_list.append(star_count)

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

您需要做两件事,使用
来定位元素而不是
浏览器
,并将
添加到
xpath
来指定当前上下文

stars = row.find_elements_by_xpath('.//span/span/span[contains(@style,"width:100%")]')

要仅搜索一行,您必须使用
而不是
浏览器

stars = row.find_elements_by_xpath(...)

EDIT:我忘了使用
来创建相对于
行的
xpath
而不是使用绝对
xpath

ratingcolumn = browser.find_elements_by_xpath('//div[contains(@class,"qr0ren-7 euifNX")]')

for row in ratingcolumn:
    stars = row.find_elements_by_xpath('.//span[contains(@style,"width:100%")]')

    star_count_list.append(len(stars))
顺便说一句:您还可以使用
/
跳过一些标记


您可以使用
len(start)
而不是
-循环计数列表
中的元素

来限制您应该使用
行。查找
而不是
浏览器。查找
非常感谢!我试过了,结果是每个评级栏的结果都是“0”星。我试过去掉“.”,每个评级栏的结果都是“22”。我可以知道原因吗?@JoeyWong
已经是
“qr0ren-7 euifNX”,您需要从子元素开始搜索(更新了答案)
xpath`从根开始搜索
告诉它根是您正在使用的
WebElement
。它成功了!非常感谢你的帮助!祝你在新的一年里一切顺利!谢谢!我试过这个,结果是每个评级栏都有“22”颗星。我可以知道为什么吗?我不知道xpath中的
如何使用相对于行的xpath,而不是绝对xpath。我使用
添加了代码。我还使用
len()
来计算星星数