【发布时间】:2019-11-12 23:15:51
【问题描述】:
您好,我希望能够从该站点获取价格,将它们解析为整数,然后平均它们。尝试了几种方法,但一直在努力解析出最终数字。
import requests
from bs4 import BeautifulSoup
URL = 'https://www.watchfinder.co.uk/search?q=114060&orderby=AgeNewToOld'
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
prices = soup.find_all(class_=('prods_price'))
for price in prices:
price = price.text
print(price)
这给了我:
£9,450
£8,750
£8,450
有没有办法平均它们?对不起各位,谢谢!
【问题讨论】:
-
...struggling to parse out the final numbers- 你的意思是你不能从soup中提取你需要的数据吗?您所做的只是打印price,您不应该将其转换为浮点数,将其放入容器中,将容器中的值相加然后除吗? -
您应该提供
page.content- minimal reproducible example 的最小示例。 -
我打印只是为了了解正在发生的事情。我正在努力达到转换的目的
-
我现在有 9450 8750 8450 - 如何将它们分开?
标签: python python-3.x list beautifulsoup