如何计算价格的数量python
How to count the amount of prices python
我试图从我成功抓取的网页中抓取一些价格
prices = item.find_all("span", {"class": "price"})
for price in prices:
price_end = price.text.strip().replace(",","")[2:]
print(price_end)
输出为:
13
36
50
65
12
52
60
85
结果我一共有8个价格。我的问题是,如何使用 Python 自动计算输出中有多少价格?
我用 len 试过了,但它只给出了相应数字的长度。
看起来很直,但我一直运行进墙。
你们能帮帮我吗?感谢任何反馈。
count = 0
prices = item.find_all("span", {"class": "price"})
for price in prices:
price_end = price.text.strip().replace(",","")[2:]
count += 1
print(price_end)
print(count, " prices found")
您可以将它们保存在列表中:
price_list=[]
prices = item.find_all("span", {"class": "price"})
for price in prices:
price_end = price.text.strip().replace(",","")[2:]
price_list.append(price_end)
print(len(price_list))
print('\n'.join(price_list))
(len(prices)
如果您为每个条目设置一个价格,也可能有效...)
您可能希望将价格存储在列表中。这是使用 for 循环的另一种方法。这称为列表理解:
prices = [
price.text.strip().replace(",","")[2:]
for price in item.find_all("span", {"class": "price"})
]
这是一张价格表。然后您可以打印价格数量和每个价格(此处使用字符串格式):
print("{price_count} prices: {prices}".format(
price_count=len(price_list)),
prices=prices,
)
我试图从我成功抓取的网页中抓取一些价格
prices = item.find_all("span", {"class": "price"})
for price in prices:
price_end = price.text.strip().replace(",","")[2:]
print(price_end)
输出为:
13
36
50
65
12
52
60
85
结果我一共有8个价格。我的问题是,如何使用 Python 自动计算输出中有多少价格?
我用 len 试过了,但它只给出了相应数字的长度。
看起来很直,但我一直运行进墙。
你们能帮帮我吗?感谢任何反馈。
count = 0
prices = item.find_all("span", {"class": "price"})
for price in prices:
price_end = price.text.strip().replace(",","")[2:]
count += 1
print(price_end)
print(count, " prices found")
您可以将它们保存在列表中:
price_list=[]
prices = item.find_all("span", {"class": "price"})
for price in prices:
price_end = price.text.strip().replace(",","")[2:]
price_list.append(price_end)
print(len(price_list))
print('\n'.join(price_list))
(len(prices)
如果您为每个条目设置一个价格,也可能有效...)
您可能希望将价格存储在列表中。这是使用 for 循环的另一种方法。这称为列表理解:
prices = [
price.text.strip().replace(",","")[2:]
for price in item.find_all("span", {"class": "price"})
]
这是一张价格表。然后您可以打印价格数量和每个价格(此处使用字符串格式):
print("{price_count} prices: {prices}".format(
price_count=len(price_list)),
prices=prices,
)