如何使用 selenium 和 python 一次打印 2 个值?

发布于 2025-01-11 23:39:25 字数 1135 浏览 0 评论 0原文

我希望每个人都有美好的一天。我正在尝试从网站中提取值并将它们打印为列表,但我不知道该怎么做。我按预期打印了所有值,只是不知道如何让它们一个接一个地打印。我知道这是一个非常基本的问题,但我无法弄清楚。任何建议或信息表示赞赏!谢谢你!

import time
import webbrowser
from os import O_SEQUENTIAL, link
import chromedriver_autoinstaller
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

webdriver = wd.Chrome(executable_path= r"C:\Users\Stephanie\anaconda3\pkgs\python-chromedriver-binary-98.0.4758.48.0-py39hcbf5309_0\Lib\site-packages\chromedriver_binary\chromedriver.exe")
webdriver.implicitly_wait(1)
webdriver.maximize_window()

webdriver.get("https://pcpartpicker.com/user/stephwaters/saved/#view=HgH2xr")
time.sleep(2)

partname = webdriver.find_elements(By.CLASS_NAME, 'td__component')
for part in partname:
    print(part.text + ': ')


prices = webdriver.find_elements(By.CLASS_NAME, 'td__price')
for price in prices:
    print(price.text)

这是输出: 输出

我希望它打印: 外壳:168.99 美元 电源:182.00 美元

等。

I hope everyone is having a good day. I am trying to extract values from a website and have them print out as a list, but I can't figure out how to do that. I have all the values printing as expecting, just can't figure out how to have them print one after another. I know this is a very basic question, but I can't figure it out. Any advice or information is appreciated! Thank you!

import time
import webbrowser
from os import O_SEQUENTIAL, link
import chromedriver_autoinstaller
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

webdriver = wd.Chrome(executable_path= r"C:\Users\Stephanie\anaconda3\pkgs\python-chromedriver-binary-98.0.4758.48.0-py39hcbf5309_0\Lib\site-packages\chromedriver_binary\chromedriver.exe")
webdriver.implicitly_wait(1)
webdriver.maximize_window()

webdriver.get("https://pcpartpicker.com/user/stephwaters/saved/#view=HgH2xr")
time.sleep(2)

partname = webdriver.find_elements(By.CLASS_NAME, 'td__component')
for part in partname:
    print(part.text + ': ')


prices = webdriver.find_elements(By.CLASS_NAME, 'td__price')
for price in prices:
    print(price.text)

This is the output:
Output

I would like it to print:
Case: $168.99
Power Supply: $182.00

and so on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2025-01-18 23:39:25

您可以迭代从每个产品中提取名称和价格的产品列表,而不是分别获取零件名称价格
此外,建议使用预期条件显式等待,而不是硬编码的暂停。
你的代码可能是这样的:

import time
import webbrowser
from os import O_SEQUENTIAL, link
import chromedriver_autoinstaller
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

webdriver = wd.Chrome(executable_path= r"C:\Users\Stephanie\anaconda3\pkgs\python-chromedriver-binary-98.0.4758.48.0-py39hcbf5309_0\Lib\site-packages\chromedriver_binary\chromedriver.exe")
wait = WebDriverWait(webdriver, 20)
webdriver.maximize_window()

webdriver.get("https://pcpartpicker.com/user/stephwaters/saved/#view=HgH2xr")
wait.until(EC.visibility_of_element_located((By.XPATH, "//tr[@class='tr__product']")))
time.sleep(0.3) #short delay added to make sure not the first product only got loaded
products = = webdriver.find_elements(By.XPATH, '//tr[@class="tr__product"]')
for product in products:
    name = product.find_element(By.XPATH, './/td[@class="td__component"]')
    price = product.find_element(By.XPATH, './/td[@class="td__price"]//a') 
    print(name.text + ': ' + price.text)

Instead of getting the partnames and prices separately you can iterate over a list of products extracting from each one it's name and price.
Also it's recommended to use Expected Conditions explicit waits, not a hardcoded pauses.
Your code could be something like this:

import time
import webbrowser
from os import O_SEQUENTIAL, link
import chromedriver_autoinstaller
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

webdriver = wd.Chrome(executable_path= r"C:\Users\Stephanie\anaconda3\pkgs\python-chromedriver-binary-98.0.4758.48.0-py39hcbf5309_0\Lib\site-packages\chromedriver_binary\chromedriver.exe")
wait = WebDriverWait(webdriver, 20)
webdriver.maximize_window()

webdriver.get("https://pcpartpicker.com/user/stephwaters/saved/#view=HgH2xr")
wait.until(EC.visibility_of_element_located((By.XPATH, "//tr[@class='tr__product']")))
time.sleep(0.3) #short delay added to make sure not the first product only got loaded
products = = webdriver.find_elements(By.XPATH, '//tr[@class="tr__product"]')
for product in products:
    name = product.find_element(By.XPATH, './/td[@class="td__component"]')
    price = product.find_element(By.XPATH, './/td[@class="td__price"]//a') 
    print(name.text + ': ' + price.text)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文