【问题标题】:Extracting data from BeautifulSoup result set [closed]从 BeautifulSoup 结果集中提取数据 [关闭]
【发布时间】:2016-12-29 12:50:46
【问题描述】:

我过滤了这个标签 和 数据 = soup.findAll('div',{'id':'responseDiv'}) 并得到了这个。

{"valid":"true","isinCode":null,"lastUpdateTime":"29-DEC-2016 12:19:23","ocLink":"/marketinfo/sym_map/symbolMapping.jsp?symbol =NIFTY&instrument=-&date=-&segmentLink=17&symbolCount=2","tradedDate":"29DEC2016","data":[{"change":"18.65","sellPrice1":"8,050.90","buyQuantity3":"75 ","sellPrice2":"8,050.95","buyQuantity4":"225","buyQuantity1":"750","ltp":"-","buyQuantity2":"150","sellPrice5":"8,051.15", "sellPrice3":"8,051.00","buyQuantity5":"675","sellPrice4":"8,051.05","底层证券":"NIFTY","bestSell":"-","annualisedVolatility":"16.61","optionType ":"-","prevClose":"8,031.35","pChange":"0.23","lastPrice":"8,050.00","lowPrice":"8,025.00","strikePrice":"-","premiumTurnover": "-","numberOfContractsTraded":"54,112","underlyingValue":"8,055.20","openInterest":"1,03,46,700","impliedVolatility":"-","vwap":"8,046.98" ,"totalBuyQuantity":"5,20,350","openPrice":"8,028.00","closePrice":"0.00","bestBuy":"-","changeinOpenInterest":"-2,11,050", "clientWisePositionLimits":"29320076","totalSe llQuantity":"9,75,675","dailyVolatility":"0.87","sellQuantity5":"225","marketLot":"75","expiryDate":"29DEC2016","marketWidePositionLimits":"-"," sellQuantity2":"150","sellQuantity1":"75","buyPrice1":"8,050.00","sellQuantity4":"150","buyPrice2":"8,049.80","sellQuantity3":"450","buyPrice4" :"8,049.30","buyPrice3":"8,049.35","buyPrice5":"8,049.15","turnoverinRsLakhs":"3,26,578.64","pchangeinOpenInterest":"-2.00","settlementPrice":"8031.35","instrumentType ":"FUTIDX","highPrice":"8,060.00"}],"companyName":"Nifty 50","eqLink":""}

]

我想提取粗体文本。我只是将整个内容转换为字符串并通过索引调用。我确信有一种正确的方法来转换结果集

【问题讨论】:

    标签: beautifulsoup resultset


    【解决方案1】:

    您的问题有点不清楚,需要编辑,但该响应看起来像 json.你可以加载它

    import json
    
    ...
    data = soup.findAll('div',{'id':'responseDiv'})
    

    假设您真正从 findAll 得到的是一个列表,其中包含包含该 json 文本的元素。

    extracted = json.loads(data[0].getText())
    print(extracted['data'][0]['vwap'])
    

    8,046.98

    您尝试提取的“vwap”可以像这样访问。 extracted 是一个字典,它有一个带有关键字 'data' 的列表,该列表的第 0 个元素是一个字典,而在关键字 'vwap' 的里面是信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 2022-07-11
      • 2021-04-28
      • 1970-01-01
      • 2019-09-09
      • 2012-02-03
      相关资源
      最近更新 更多