Selenium Web刮擦给了我错误的行,我在哪里做错了?
您好,我是网站报废网站,
这是我的代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
search_url = "https://dealt.ae/collections/laptops"
driver.get(search_url)
time.sleep(3)
ALL_ITEMS = []
item = ["", "", "", ""]
all_laptops = driver.find_elements(By.XPATH, '//div[@class="product-collection products-grid row"]')
grades = all_laptops[0].find_elements(By.XPATH, '//div[@class="product-top"]/span[@class="product-metafild"]')
for i in range(len(grades)):
item[0] = grades[i].text
description = all_laptops[0].find_elements(By.CLASS_NAME, 'product-title')
item[1] = description[i].text
old_price= all_laptops[0].find_elements(By.CLASS_NAME, 'old-price')
item[2] = old_price[i].text
special_price= all_laptops[0].find_elements(By.CLASS_NAME, 'special-price')
item[3] = special_price[i].text
print(item)
ALL_ITEMS.append(item.copy())
for item in ALL_ITEMS:
print(item)
my_df = pd.DataFrame(ALL_ITEMS)
my_df.to_csv('laptop.csv', index=False, header=False)
,它给我一个错误的输出(行),其他人的产品和其他产品的价格等级。请告诉我我在哪里做错了。
谢谢
Hello I am web scrapping a site
here is my code
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
search_url = "https://dealt.ae/collections/laptops"
driver.get(search_url)
time.sleep(3)
ALL_ITEMS = []
item = ["", "", "", ""]
all_laptops = driver.find_elements(By.XPATH, '//div[@class="product-collection products-grid row"]')
grades = all_laptops[0].find_elements(By.XPATH, '//div[@class="product-top"]/span[@class="product-metafild"]')
for i in range(len(grades)):
item[0] = grades[i].text
description = all_laptops[0].find_elements(By.CLASS_NAME, 'product-title')
item[1] = description[i].text
old_price= all_laptops[0].find_elements(By.CLASS_NAME, 'old-price')
item[2] = old_price[i].text
special_price= all_laptops[0].find_elements(By.CLASS_NAME, 'special-price')
item[3] = special_price[i].text
print(item)
ALL_ITEMS.append(item.copy())
for item in ALL_ITEMS:
print(item)
my_df = pd.DataFrame(ALL_ITEMS)
my_df.to_csv('laptop.csv', index=False, header=False)
It is giving me a wrong output (rows), with grade of someone else product and price of someone else product. Please tell me where i did wrong.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不会在循环中为每个笔记本电脑提供信息。这是一种更清洁的方法:
You're not pulling the information for each laptop within the loop. Here's a cleaner way of doing it:
仅使用API URL,请刮擦所需的数据静态方式,您还可以轻松地使分页
输出:
Just using API url, scrape the desired data static way where you also can make the pagination easily
Output:
这是因为您没有选择完美的班级名称,这是所有笔记本电脑中常见的事情,并且拥有有关该特定笔记本电脑的所有细节。
这是完整的工作代码
It is because your are not selecting the perfect class name which is a common thing in all laptops and have all the details about that particular laptop.
Here is the full working code