GVKun编程网logo

如何使用 Selenium 和 Python 抓取和计算星级?

2

对于想了解如何使用Selenium和Python抓取和计算星级?的读者,本文将提供新的信息,并且为您提供关于(selenium+python)_UI自动化01_Mac下selenium环境搭建、Pyt

对于想了解如何使用 Selenium 和 Python 抓取和计算星级?的读者,本文将提供新的信息,并且为您提供关于(selenium+python)_UI 自动化 01_Mac 下 selenium 环境搭建、Python 3 Selenium selenium.common.exceptions.WebDriverException:消息:未知错误:net::ERR_CONNECTION_RESET、Python selenium - 会话未创建:此版本的 ChromeDriver 仅支持使用 ChromeDriver 的 Chrome 版本 86 和使用 Selenium 的 Chrome、Python selenium : TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:的有价值信息。

本文目录一览:

如何使用 Selenium 和 Python 抓取和计算星级?

如何使用 Selenium 和 Python 抓取和计算星级?

如何解决如何使用 Selenium 和 Python 抓取和计算星级?

我目前正在尝试抓取此特定网站上的评论:https://shopee.sg/buyer/275295198/rating,但我无法找到使用 selenium 抓取星级评分的方法,因为每颗星星本身都是一个 svg。意识到填充的星星和未填充的星星之间存在模式:

这是我对填充的星星进行计数和求和的当前代码,但似乎只返回 1:

    # Star rating
      star_ratings = product.find_elements_by_css_selector("[shopee-product-rating__rating'']")
      stars = product.find_elements_by_css_selector("[shopee-svg-icon icon-rating-solid--active icon-rating-solid'']")

      star_rate = 0
      for rating in star_ratings:
          #print(rating.get_attribute(''svg''))
          if (product.find_elements_by_css_selector("[shopee-svg-icon icon-rating-solid--active icon-rating-solid'']")) == stars:
              star_rates = star_rate + 1
              continue
      rating_csv.append(star_rates)
      print(star_rates)

解决方法

您可以简单地使用 len()stars 来获取信息,而无需迭代结果和计数,如果它只覆盖活跃的星星:

rating_csv.append(len(stars))

示例

from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path=r''C:\\Program Files\\ChromeDriver\\chromedriver.exe'')
url = ''https://shopee.sg/buyer/208830993/rating''
driver.get(url)
time.sleep(5)

for rating in driver.find_elements_by_css_selector(''.shopee-product-rating''):
    stars = rating.find_elements_by_css_selector(''.icon-rating-solid'')
    print(len(stars))
    
driver.close()

输出

4
5
5
5
5
5

(selenium+python)_UI 自动化 01_Mac 下 selenium 环境搭建

(selenium+python)_UI 自动化 01_Mac 下 selenium 环境搭建

前言

Selenium 是一个用于 Web 网页 UI 自动化测试的开源框架,可以驱动浏览器模拟用户操作。支持多种平台 (Windows、Mac OS、Linux) 和多种浏览器 (IE、Firefox、Chrome、Safari),可以用多种语言 (Java,Ruby,Python,Perl,PHP,C#) 编写测试用例。

selenium+python 环境搭建

python 环境安装

mac 上安装 python 环境可参考:Mac 版 - python 环境配置(一):Python 下载安装

安装 selenium

打开终端 ->pip 安装(安装命令:pip3 install selenium)

安装浏览器驱动

安装 Chrome 浏览器驱动 ChromeDriver

1. 查看 Chrome 版本

设置 -> 关于 Chrome

2. 查找 Chrome 和 ChromeDriver 的对应关系

Chrome 和 ChromeDriver 的对应关系可参考:https://www.cnblogs.com/mini-monkey/p/12088721.html

3. 下载对应 ChromeDriver

ChromeDriver 下载地址:http://chromedriver.storage.googleapis.com/index.html

小编 1 中查看到的 chrome 版本为 79,故下载 ChromeDriver 79 版本

4. ChromeDriver 环境配置

进入 /usr/local/bin 目录(由于 Mac 该目录是隐藏的,所以可通过快捷键 command+shift+g 打开)

将解压的 ChromeDriver 拖拽到 bin 文件夹中

查看 ChromeDriver 版本:chromedriver --version

备注:输出 ChromeDriver 版本即安装成功

5. 编写脚本启动 Chrome 浏览器

1 # 启动Chrome
2 from selenium import webdriver
3 
4 driver = webdriver.Chrome()
5 driver.get(''http://www.baidu.com/'')  # 打开百度

运行脚本,打开百度页

 

Python 3 Selenium selenium.common.exceptions.WebDriverException:消息:未知错误:net::ERR_CONNECTION_RESET

Python 3 Selenium selenium.common.exceptions.WebDriverException:消息:未知错误:net::ERR_CONNECTION_RESET

如何解决Python 3 Selenium selenium.common.exceptions.WebDriverException:消息:未知错误:net::ERR_CONNECTION_RESET

我正在尝试从不安全的网站下载多个 PDF。 它运行良好,但是,一旦我下载了 20 个 pdf,就会出现此错误。 如果每次下载后我 time.sleep(3) ,此错误就会消失。但我有大约 15000 个 PDF 可供下载。 我正在使用最新的 selenium、最新的 chrome webdriver 和 python 3.8

你有什么想法吗? :)

Python selenium - 会话未创建:此版本的 ChromeDriver 仅支持使用 ChromeDriver 的 Chrome 版本 86 和使用 Selenium 的 Chrome

Python selenium - 会话未创建:此版本的 ChromeDriver 仅支持使用 ChromeDriver 的 Chrome 版本 86 和使用 Selenium 的 Chrome

如何解决Python selenium - 会话未创建:此版本的 ChromeDriver 仅支持使用 ChromeDriver 的 Chrome 版本 86 和使用 Selenium 的 Chrome

运行我的代码时出现此错误:

  1. This version of ChromeDriver only supports Chrome version 86

当前浏览器版本为 88.0.4324.104,二进制路径为 C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe

是不是我的版本太大了?

解决方法

您使用的版本已过时,请在浏览器中输入“version://chrome”并查看您拥有的确切版本。然后转到 chromedriver download 并下载适合您的版本。

正如您的错误所说,您的版本是“88.0.4324.104”,所以 download this one 应该可以使用。

,

您可以从此链接下载 chrome 驱动程序版本 86 https://chromedriver.storage.googleapis.com/index.html?path=86.0.4240.22/

,

这个错误信息...

  1. This version of ChromeDriver only supports Chrome version 86 Current browser version is 88.0.4324.104 with binary path C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe

...表示您使用的是 ChromeDriver v86.x,而您的 Chrome 浏览器版本是 v88.0.4324.104强>.

您的主要问题是您使用的二进制文件版本之间的不兼容,如下所示:

  • 您正在使用 chromedriver=86.0
  • chromedriver=86.0 的发行说明清楚地提到了以下内容:

支持 Chrome 86 版

  • 您正在使用 chrome=88.0.4324.104
  • ChromeDriver 88.0.4324.96 的发行说明清楚地提到了以下内容:

支持 Chrome 88 版

因此 chromedriver=86.0ChromeDriver 88.0.4324.96

之间存在明显的不匹配

解决方案

确保:

  • ChromeDriver 已更新至当前 ChromeDriver v88.0.4324.96 级别。
  • 非root用户身份执行您的@Test
  • 始终在 driver.quit() 方法中调用 tearDown(){} 以优雅地关闭和销毁 WebDriverWeb Client 实例。

Python selenium : TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

Python selenium : TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

如何解决Python selenium : TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

我在 Python 中使用 selenium 时遇到此错误,不知道如何解决,我尝试使用 Chromeenter image description here 和 Firefox,同样的事情:

enter image description here

回溯(最近一次调用最后一次): 文件“C:/python/bot_test/fast_buy_FF.py”,第 56 行,在 checkout = webdriverwait(driver,10).until(EC.presence_of_element_located((By.ID,"turbo-checkout-pyo-button"))) 文件“C:\\Users\\basheer.kafaf\\AppData\\Local\\Programs\\Python\\python37\\lib\\site-packages\\selenium\\webdriver\\support\\wait.py”,第 80 行,直到 引发超时异常(消息,屏幕,堆栈跟踪) selenium.common.exceptions.TimeoutException:消息:

代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.support import expected_conditions as EC
import time
from playsound import playsound

# PATH = ''chromedriver.exe''
driver = webdriver.Firefox()



Max_price = 800

Item_code = ''B08N5D31XF''
Item_http_offer_page = ''https://www.amazon.com/dp/{}?smid=ATVPDKIKX0DER&tag=data20-20#aod''.format(Item_code)
Item_http_Product_page = ''https://www.amazon.com/gp/product/{}?tag=ns0ec-20''.format(Item_code)

log_in_email = "XXXXXXXXXXX"
passward = "XXXXXXXXXX"
driver.get("https://www.amazon.com/ref=nav_logo")

sgin_in = webdriverwait(driver,"nav-signin-tooltip")))
sgin_in.click()

ap_email = webdriverwait(driver,2).until(EC.presence_of_element_located((By.ID,"ap_email")))
ap_email.send_keys(log_in_email)
search1 = driver.find_element_by_id("continue")
search1.click()

ap_password = webdriverwait(driver,"ap_password")))
ap_password.send_keys(passward)
search2 = driver.find_element_by_id("signInSubmit")
search2.click()


time.sleep(1)
New_tab = driver.find_element_by_tag_name(''body'').send_keys(Keys.CONTROL + ''t'')
driver.get(Item_http_Product_page)


Looper = True

while Looper:
    # try:
        buyBox = webdriverwait(driver,"price_inside_buyBox")))
        print (buyBox.text)
        item_price = int(float(buyBox.text.replace('','','''').replace(''$'','''')))
        print (item_price)

        if  item_price < Max_price:
            # playsound(''bells.mp3'')
            buy_Now= webdriverwait(driver,4).until(EC.presence_of_element_located((By.ID,"buy-Now-button")))
            buy_Now.click()
            checkout = webdriverwait(driver,"turbo-checkout-pyo-button")))
            checkout.click()
            Looper = False



    # except:
    #     print(''No buy Now available yet '')
    #     time.sleep(1)
    #     driver.refresh()

解决方法

这是因为打开了一个新框架 我需要切换到新框架。

所以我添加了以下代码以切换到新框架。

driver.switch_to.frame(WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,"iframe"))))

关于如何使用 Selenium 和 Python 抓取和计算星级?的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于(selenium+python)_UI 自动化 01_Mac 下 selenium 环境搭建、Python 3 Selenium selenium.common.exceptions.WebDriverException:消息:未知错误:net::ERR_CONNECTION_RESET、Python selenium - 会话未创建:此版本的 ChromeDriver 仅支持使用 ChromeDriver 的 Chrome 版本 86 和使用 Selenium 的 Chrome、Python selenium : TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:等相关知识的信息别忘了在本站进行查找喔。

本文标签: