我正试着从给定的网址上搜集曲棍球棒的价格。最后,我也想抓取名称+网址,但我不认为有必要解决这个问题
我得到的是:
import requests
from pandas.io.json import json_normalize
from bs4 import BeautifulSoup
url = 'https://www.prohockeylife.com/collections/senior-hockey-sticks'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
stick_names = soup.find_all(class_='product-title')
stick_prices = soup.find_all(class_='regular-product')
print(stick_prices)
上面的代码成功地返回了曲棍球棒的价格,但看起来是这样的:
[<p class="regular-product">
<span>$319.99</span>
</p>, <p class="regular-product">
<span>$339.99</span>
</p>, <p class="regular-product">
<span>$319.99</span>
我想把它清理一下,只退回实际价格
我试过一些方法,包括:
dirty_prices = soup.find_all(class_='regular-product')
clean_prices = dirty_prices.get('a')
print(clean_prices)
但收效甚微。感谢指点
您需要.text属性,您也可以在列表理解期间提取该属性。然后list/zip在末尾显示名称/价格的元组列表
不确定,但我认为以下是您可能需要的:
不要使用
print(stick_prices)
,而是使用:输出的开始是:
等等
相关问题 更多 >
编程相关推荐