在Beautifulsoup中解析json字符串时无法获取浮点值

时间:2018-10-17 11:31:47

标签: python json beautifulsoup

我是网络爬虫的新手,我正在使用python和beautifulsoup从网页中获取一些详细信息,并且能够从中获取一些json字符串。但这并没有给定"shippingPrice": 5.99的浮点值,因此请有人帮助我获得相同的值。

这是我的源代码-

from bs4 import BeautifulSoup
import requests
import re
import json

url = "https://www.walmart.com/ip/Mainstays-Black-5-Cup-Coffee-Maker-with-Removable-Filter-Basket/53056868"

s = requests.session()  #start requests session    
page = s.get(url)  #get the page
soup = BeautifulSoup(page.content,features="lxml")  #parse the html

pathInfo = soup.find_all('script')[6]
data = re.search(r"window.__WML_REDUX_INITIAL_STATE__ = (.*?);$",pathInfo.text).group(1)

if(len(data)):
    pInfo = json.loads(data[:-2])        
    selectedProductId = pInfo["productBasicInfo"]["selectedProductId"]
    offerId = pInfo["product"]["products"][selectedProductId]["offers"][0]
    shippingPrice = pInfo["product"]["offers"][offerId]["shippingPrice"]       

    print(shippingPrice)

0 个答案:

没有答案