如何使用Python打印每一行

如何使用Python打印每一行,python,pandas,selenium,ubuntu,selenium-chromedriver,Python,Pandas,Selenium,Ubuntu,Selenium Chromedriver,如何打印“日期不可用”和缺少熊猫行,就像我通常在网页上所做的那样?这是我的代码,它提供了两种不同的输出: 从selenium导入webdriver browser=webdriver.Chrome(可执行文件路径=“./drivers/chromedriver”) browser.get('https://www.ebay.it/sch/i.html?_from=R40&_nkw=3060&_sacat=0&_sop=15') 导入时间 时间。睡眠(2) cookie=浏览器。按id(“gdpr

如何打印“日期不可用”和缺少熊猫行,就像我通常在网页上所做的那样?这是我的代码,它提供了两种不同的输出:

从selenium导入webdriver
browser=webdriver.Chrome(可执行文件路径=“./drivers/chromedriver”)
browser.get('https://www.ebay.it/sch/i.html?_from=R40&_nkw=3060&_sacat=0&_sop=15')
导入时间
时间。睡眠(2)
cookie=浏览器。按id(“gdpr横幅接受”)查找元素。单击()
page\u source=browser.page\u source
从bs4导入BeautifulSoup
汤=美汤(第页\来源)
soup=BeautifulSoup(browser.page\u来源)
results=(soup.find(“ul”,“class”:“srp results”}))
结果:findAll(“li”)
项目=结果。最终结果(“li”)
行=[]
对于项目中的项目:
titleElement=项目。查找(“h3”)
priceElement=item.find(“span”,“class”:“s-item\uuuuu price”})
dateElement=item.find(“span”,“class”:“s-item\uuu time-end”})
如果标题元素:
newListingTag=titleElement.find(“span”,{“class”:“LIGHT_HIGHLIGHT”})
如果是newListingTag:
newListingTag.extract()
打印(titleElement.text)
如果是priceElement:
打印(priceElement.text)
如果是dateElement:
打印(dateElement.text)
其他:
打印(“日期不可用”)
结果:findAll(“li”)
项目=结果。最终结果(“li”)
行=[]
对于项目中的项目:
titleElement=项目。查找(“h3”)
priceElement=item.find(“span”,“class”:“s-item\uuuuu price”})
dateElement=item.find(“span”,“class”:“s-item\uuu time-end”})
如果titleElement和priceElement以及dateElement:
newListingTag=titleElement.find(“span”,{“class”:“LIGHT_HIGHLIGHT”})
如果是newListingTag:
newListingTag.extract()
行=[dateElement.text,titleElement.text,priceElement.text]
行。追加(行)
作为pd进口熊猫
pd.set_选项('expand_frame_repr',False)
pd.set_选项(“display.max_行”,无,“display.max_列”,无,“display.max_列宽”,无)
pd.set_选项('display.max_colwidth',-1)
df=pd.DataFrame.from_记录(行、列=[“购买日期”、“标题”、“价格”])
打印(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。ecc。(包括大约50个元素)


另一方面,这是第二个输出(与第一个输出相比,它缺少大约34个元素,为什么?)

ecc。ecc

此外,我注意到第二个输出只显示带有确切日期的行


感谢您的回答,并为我糟糕的英语表示歉意

我对Selenium不是非常熟悉,但在查看该网站后,似乎并非所有列表都会包含类
s-item\u time-end
。要包含的另一个标签可能是:
s-item\uuu purchase-options-with-icon
,它允许立即购买和其他变体。只需添加另一个变量,如下所示:

dateElement=item.find(“span”,“class”:“s-item\uu time-end”})

altDateElement=item.find(“span”,“class”:“s-item\uu purchase-options-with-icon”})

对于原始设置,如果
dateElement
为None,验证检查可能会失败,如下所示:

如果titleElement和priceElement以及dateElement

您还可以使用
dateElement
尝试一个空字符串缓冲区,以查看是否允许空白结果


dateElement=item.find(“span”,“class”:“s-item\uuu time-end”})或“

我添加了这样一个if语句来获取每个产品的名称:if item.find(“span”,“class”:“s-item\uu time-end”}):dateElement=item.find(“span”,“class”:“s-item\uu time-end”})else:dateElement=item.find(“span”{“class”:“s-item\uuu shipping s-item\uuu logisticsCost”})现在,我想写“notavailable”而不是item.find(“span”,{“class”:“s-item\uu shipping s-item\uu logisticsCost”})
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
...