【问题标题】:I want to get the printed output in a list like [output, output]我想在 [output, output] 之类的列表中获取打印输出
【发布时间】: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


【解决方案1】:

试试:

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")

titles = []
prices = []
for index, tp in enumerate(zip(title, price)):
    if index >= 10:
        break
    titles.append(tp[0].get_text(strip=True))
    prices.append(tp[1].get_text(strip=True))

print(titles)
print(prices)

创建两个列表titlesprices 并打印它们:

['Pepsi Pepsi Max - 330Ml', 'Pepsi Regular Bottle 330ml', 'Pepsi Black Regular Bottle 330ml', 'Pepsi Diet Bottle 2.25L', 'Pepsi Regular 2.25L', 'Pepsi Regular Can 185ml', 'Pepsi Diet Bottle 1.25L', 'Pepsi Diet Can 185ml', 'Pepsi Regular Bottle 1.25L', 'Pepsi Diet Pet - 330Ml']
['LBP 17,999', 'LBP 3,999', 'LBP 3,999', 'LBP 10,999', 'LBP 10,999', 'LBP 4,999', 'LBP 8,999', 'LBP 4,999', 'LBP 8,999', 'LBP 3,999']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2018-12-04
    • 2011-07-25
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多