简体   繁体   English

试图 web 抓取动态网页,没有得到错误代码。 如何验证“它正在工作”?

[英]Trying to web scrape a dynamic webpage, didn't get an error code. How can I verify “it's working”?

I'm trying to webscrape a dynamic website with the following code below.我正在尝试使用以下代码对动态网站进行网页抓取。 My first few attempts returned some error messages like "return self.find_element(by=By.CLASS_NAME, value=name)" and some "Message: no such element: Unable to locate element".我最初的几次尝试返回了一些错误消息,例如“return self.find_element(by=By.CLASS_NAME, value=name)”和一些“消息:没有这样的元素:无法找到元素”。 However, now it has been working but hasn't given me the results I want.但是,现在它一直在工作,但没有给我想要的结果。 What element should I be targeting?我应该定位什么元素? Is it possible something is wrong with my setup of selenium and chromedriver(had trouble with these initially too).我的 selenium 和 chromedriver 的设置是否可能有问题(最初也遇到了问题)。

   from selenium import webdriver
   url = 'https://www.landers.ph/beverages/coffee.html'
   driverlocation = '/Users/username/Downloads/chromedriverdriver = 
   webdriver.Chrome(driverlocation)
   driver.get(url)
   products = driver.find_elements_by_class_name('ld-product-detail-info')

   for product in products:
       name = product.find_element_by_class_name('productTitle').text
       price = product.find_element_by_class_name('priceComponent').text
       print(name,price)

Induce WebDriverWait () and wait for visibility_of_all_elements_located () and following class name诱导WebDriverWait () 并等待visibility_of_all_elements_located () 并遵循class name

driver.get('https://www.landers.ph/beverages/coffee.html')
products=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME,"productComponent")))
for product in products:
    name = product.find_element_by_class_name('productTitle').text
    price = product.find_element_by_class_name('priceComponent').text
    print(name,price)

Output : Output

Cafe Puro 100% Pure Instant Coffee in Mason Jar 90g ₱99.75
Cafe Puro 3-in-1 Instant Coffee 20 x 17g ₱85.75
San Mig Sugar Free White 3 in 1 Coffee 10 x 9g ₱61.75
Nescafe Classic Double Filter Instant Coffee Pack 200g ₱144.75
Nescafe Original Instant Coffee Mix 840g ₱171.75
Nescafe Creamy White 3-in-1 Instant Coffee 870 g ₱175.75
San Mig Super 3-in-1 Sugar Free Coffee 20 pcs x 7 g ₱111.75
Good Day Vanilla Latte 3 in 1 Instant Coffee 30pcs. x 20 g ₱139.75
Good Day Cappuccino with Coklat Granule 30 x 25g ₱199.75
Gold Choice Original Coffee Mix 20x 20g ₱149.75
Kopiko Black 3-in-1 Instant Coffee Mix 30 x 30g ₱209.25
San Mig Original 3-in-1 Coffee Mix 30 x 20g ₱159.75
Nescafe Gold Latte Macchiato 10 x 20g ₱169.75
Nescafe Classic Decaf Resealable Pack Instant Coffee 160 g ₱162.75
Cafe Puro Jade Blend Fresh Ground Coffee in a Tin Can 400g ₱188.75
Kopiko Brown Coffee Mix 30 x 27.5g ₱210.75
Pure Nature 12-in-1 Green Coffee Mix 10 x 21g ₱279.75
Kopiko Blanca Creamy Coffee Mix 30 x 30g ₱210.75
Folgers Classic Medium Roast Ground Coffee 320 g ₱349.75
Kopiko Cappuccino Coffee Mix 30 x 25g ₱210.75
Check Hup 3-in-1 Ipoh White Coffee King 15 x 40 g ₱318.75
Nescafe Taster's Choice French Roast Instant Coffee 198g ₱619.75
Nescafe Taster's Choice House Blend Decaf Coffee 7 oz ₱679.75
Maxwell House Cappuccino 750 g ₱899.75

You need to import below libraries.您需要导入以下库。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我看不到代码有什么问题。 我不能写一个简单的计算器 - I can't see what's wrong with my code. I can't write a simple calculator 尝试在 geopy 中运行地理编码器时出现 [SSL: CERTIFICATE_VERIFY_FAILED] 错误我如何验证证书以获得所需的 output? - Getting [SSL: CERTIFICATE_VERIFY_FAILED] error when trying to run geocoder in geopy how can I verify the certificate to get needed output? 无法理解这段代码中的异步点。 这是应该如何工作的吗? - Can't understand the point of async in this code. Is this how it supposed to work? 无法从网页上抓取类别标题 - Can't scrape category titles from a webpage 这是我的代码。 我试图获取用户输入的整数,通过使用映射的函数并返回多维数据集结果的列表 - Here's my code. I am trying to get user input of integers, go through a function utilizing map and returning a list of cubed results Python 3 - web 使用 JSONDecodeError 抓取 Microsoft CVE 网页错误 - Python 3 - web scrape Microsoft CVE webpage error with JSONDecodeError 运行此 python 代码时出现错误。 错误是“ElementNotInteractableException”。 任何人都可以帮助我吗? - I am getting error while running this python code. The error is “ElementNotInteractableException”. Can any on help me out? 如何获得在此代码中工作的坐标? - How can I get coordinates working in this code? 你好。 我在执行代码时遇到了问题。 但是我得到一个错误'TypeError:只能将最后一个指针连接到str(不是“int”)到str' - Hi. I faced a problem while excuting my code. But I get an error 'TypeError: can only concatenate str (not "int") to str' for the last pointer 如何使用正则表达式来帮助抓取 web 数据? - How can I use regex to help scrape web data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM