【发布时间】:2021-07-15 10:49:42
【问题描述】:
这是我的脚本的代码,我希望输出以列表的形式返回,而不是现在输出的内容(如下所示)
import requests
from bs4 import BeautifulSoup
webpage = requests.get("https://www.spinneyslebanon.com/catalogsearch/result/?q=pepsi")
soup = BeautifulSoup(webpage.content, 'html.parser')
title = soup.find_all('a', 'product-item-link')
price = soup.find_all('span', class_='price')
for index, tp in enumerate(zip(title, price)):
if index >= 10:
break
t, p = tp
print("{:<60} {:<30}".format(t.get_text(strip=True),
p.get_text(strip=True)))
这是输出:
Pepsi Pepsi Max - 330Ml LBP 17,999
Pepsi Regular Bottle 330ml LBP 3,999
Pepsi Black Regular Bottle 330ml LBP 3,999
Pepsi Diet Bottle 2.25L LBP 10,999
Pepsi Regular 2.25L LBP 10,999
Pepsi Regular Can 185ml LBP 4,999
Pepsi Diet Bottle 1.25L LBP 8,999
Pepsi Diet Can 185ml LBP 4,999
Pepsi Regular Bottle 1.25L LBP 8,999
Pepsi Diet Pet - 330Ml LBP 3,999
我希望它是 [Pepsi Pepsi Max - 330Ml, Pepsi Regular Bottle 330ml,Pepsi Black Regular Bottle 330ml....]
价格相同 [LBP 17,999, LBP 3,999, LBP 3,999....]
【问题讨论】:
-
到
OP,您无需在清除问题的地方编辑您的问题,甚至您尝试编辑您收到的带有lol字样的答案。请尊重那些花时间阅读您的问题并提供答案的人。另一方面,如果您害怕您的老板或其他什么,那么您为什么来这里?只是发布一个问题,然后收到一个答案,然后删除它并运行?
标签: python-3.x list web-scraping beautifulsoup