【问题标题】:How to print every rows with Pandas using Python如何使用 Python 使用 Pandas 打印每一行
【发布时间】: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


    【解决方案1】:

    我对 Selenium 不是很熟悉,但是在查看了该站点之后,似乎并非所有列表都具有 s-item__time-end 类。要包含的替代标签可能是:s-item__purchase-options-with-icon,它允许立即购买和其他变体。只需添加另一个变量,如下所示:

    dateElement = item.find("span", {"class": "s-item__time-end"})
    altDateElement = item.find("span", {"class": "s-item__purchase-options-with-icon"})

    使用原始设置,如果 dateElement 为 None,则验证检查可能会失败,如下行所示:

    if titleElement and priceElement and dateElement

    您也可以尝试使用 dateElement 的空字符串缓冲区,看看是否允许空白结果。

    dateElement = item.find("span", {"class": "s-item__time-end"}) or ""

    【讨论】:

    • 我添加一个这样的 if 语句来获取每个产品的名称: if item.find("span", {"class": "s-item__time-end"}): dateElement = item.find ("span", {"class": "s-item__time-end"}) else: dateElement = item.find("span", {"class": "s-item__shipping s-item__logisticsCost"}) 现在,我会喜欢写“不可用”而不是 item.find("span", {"class": "s-item__shipping s-item__logisticsCost"})
    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2012-04-15
    • 2020-05-21
    • 2022-10-18
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    相关资源
    最近更新 更多