【发布时间】:2021-08-25 22:13:11
【问题描述】:
如何使用 Pandas 打印“日期不可用”和缺少行,就像我通常在网页上所做的那样?这是我的代码,它给了我两个不同的输出:
from selenium import webdriver
browser = webdriver.Chrome(executable_path="./drivers/chromedriver")
browser.get('https://www.ebay.it/sch/i.html?_from=R40&_nkw=3060&_sacat=0&_sop=15')
import time
time.sleep(2)
cookie = browser.find_element_by_id("gdpr-banner-accept").click()
page_source = browser.page_source
from bs4 import BeautifulSoup
soup = BeautifulSoup(page_source)
soup = BeautifulSoup(browser.page_source)
results = (soup.find("ul", {"class": "srp-results"}))
results.findAll("li")
items = results.findAll("li")
rows = []
for item in items:
titleElement = item.find("h3")
priceElement = item.find("span", {"class": "s-item__price"})
dateElement = item.find("span", {"class": "s-item__time-end"})
if titleElement:
newListingTag = titleElement.find("span", {"class": "LIGHT_HIGHLIGHT"})
if newListingTag:
newListingTag.extract()
print(titleElement.text)
if priceElement:
print(priceElement.text)
if dateElement:
print(dateElement.text)
else:
print("The date is not available")
results.findAll("li")
items = results.findAll("li")
rows = []
for item in items:
titleElement = item.find("h3")
priceElement = item.find("span", {"class": "s-item__price"})
dateElement = item.find("span", {"class": "s-item__time-end"})
if titleElement and priceElement and dateElement:
newListingTag = titleElement.find("span", {"class": "LIGHT_HIGHLIGHT"})
if newListingTag:
newListingTag.extract()
row = [dateElement.text, titleElement.text, priceElement.text]
rows.append(row)
import pandas as pd
pd.set_option('expand_frame_repr', False)
pd.set_option("display.max_rows", None, "display.max_columns", None, "display.max_colwidth", None)
pd.set_option('display.max_colwidth', -1)
df = pd.DataFrame.from_records(rows, columns=["Purchase Date", "Title", "Price"])
print(df)
这是第一个输出:
NUOVO MSI GeForce RTX 3060 12GB GPU
EUR 488,25
(Martedì, 13:02)
GIGABYTE GeForce RTX 3060 Ti OC 8GB Eagle
EUR 499,88
(Domenica, 14:47)
MSI GTX 1650 VENTUS XS 4G OC Graphics card GF GTX 1650 4 GB GDDR5 V809-3060R
EUR 532,50
The date is not available
...
ecc。电子抄送(它包括大约 50 个元素)
另一方面,这是第二个输出(与第一个输出相比,它缺少大约 34 个元素,为什么?)
0 (Martedì, 13:02) NUOVO MSI GeForce RTX 3060 12GB GPU
EUR 488,25
1 (Domenica, 14:47) GIGABYTE GeForce RTX 3060 Ti OC 8GB Eagle
EUR 499,88
2 (Giovedì, 22:11) Inno 3d GeForce RTX 3060 TWIN x2 OC 12gb GDDR 6 GPU neu&ovp - spedizione veloce ✅ EUR 510,00
...
ecc。抄送。
此外,我注意到第二个输出仅显示具有确切日期的行。
感谢您的回答,抱歉我的英语不好
【问题讨论】:
标签: python pandas selenium ubuntu selenium-chromedriver