【问题标题】:Could not fetch value from key inside list of dictionaries converted from BeautifulSoup ResultSet无法从从 BeautifulSoup ResultSet 转换的字典列表中的键中获取值
【发布时间】:2020-06-01 04:00:58
【问题描述】:

我使用 BeautifulSoup 从 https://api.huobi.pro/v1/common/symbols 获取列表的字符串表示形式

url 中的部分字符串如下所示:

 {"status":"ok","data":[{"base-currency":"ont","quote-currency":"btc","price-precision":8,"amount-precision":4,"symbol-partition":"innovation","symbol":"ontbtc","state":"online","value-precision":8,"min-order-amt":0.01,"max-order-amt":100000,"min-order-value":0.0001,"leverage-ratio":2}, .. ]

这就是我将它刮成 ResultSet 的方式:

import requests 
from bs4 import BeautifulSoup
import re

url = 'https://api.huobi.pro/v1/common/symbols'
res = requests.get(url)
html_page = res.content


soup = BeautifulSoup(html_page, 'html.parser',from_encoding='utf-8')
text = soup.find_all(text=True)

然后我尝试将 ResultSet 转换为列表

huobiList = list(text)

之后,我尝试从列表中的symbol 键打印值:

print([d[`symbol`] for d in huobiList])

但出现此错误:

print([d['symbol'] for d in huobiList])
TypeError: string indices must be integers

当我尝试使用索引打印键时,它会将键读取为字母,而不是读取整个键名:

>>>print([d[0] for d in huobiList])
[u'{']

【问题讨论】:

    标签: list python-2.7 dictionary beautifulsoup


    【解决方案1】:

    其实这里有很多问题

    symbol 键位于您要打印的对象内,但位于 data 键下方

    在此之前,您必须使用 json.loads() 将字符串转换为 dict

    检查此代码:

    import requests 
    from bs4 import BeautifulSoup
    import re
    import json
    
    url = 'https://api.huobi.pro/v1/common/symbols'
    res = requests.get(url)
    html_page = res.content
    
    
    soup = BeautifulSoup(html_page, 'html.parser',from_encoding='utf-8')
    text = soup.find_all(text=True)
    huobiList = list(text)
    datas = [json.loads(d).get('data') for d in huobiList]
    for data in datas:
        print([d.get('symbol') for d in data])
    

    结果

    ['bfteth', 'kncbtc', 'nexoeth', 'mexbtc', 'bsvhusd', 'paybtc', 'icxeth', 'aebtc', 'bixeth', 'forusdt', 'itcusdt', 'abtbtc', 'btgbtc', 'eosht', 'hitbtc', 'dogeusdt', 'yeebtc', 'buteth', 'thetausdt', 'etneth', 'akrousdt', 'acteth', 'egtusdt', 'pcbtc', 'astbtc', 'oneht', 'renusdt', 'shebtc', 'naseth', 'aidoceth', 'bhdht', 'steemeth', 'sspeth', 'neobtc', 'ostbtc', 'qtumusdt', 'dgdbtc', 'idtbtc', 'btteth', 'venbtc', 'eoseth', 'dtausdt', 'ncashbtc', 'xtzbtc', 'ltcht', 'rteeth', 'utketh', 'iostusdt', 'zlabtc', 'nodeusdt', 'gtcbtc', 'bsvbtc', 'cnneth', 'scbtc', 'venusdt', 'edubtc', 'neousdt', 'ruffbtc', 'xmreth', 'crobtc', 'btseth', 'iostbtc', 'mcobtc', 'yccbtc', 'bchbtc', 'bsvusdt', 'itcbtc', 'cvcoinbtc', 'xmrusdt', 'xtzusdt', 'qtumbtc', 'cnnsbtc', 'thetabtc', 'dogebtc', 'gasbtc', 'bcveth', 'ruffusdt', 'skmbtc', 'topceth', 'reneth', 'uceth', 'bixusdt', 'gnxbtc', 'zjlteth', 'btchusd', 'gtusdt', 'wxtusdt', 'btcusdt', 'ctxceth', 'seelebtc', 'sceth', 'edueth', 'tnteth', 'hptbtc', 'ogobtc', 'rdnbtc', 'egccbtc', 'ontusdt', 'datbtc', 'daceth', 'nulseth', 'cvcoineth', 'dtaeth', 'lymbtc', 'grsbtc', 'idteth', 'xvgeth', 'dgdeth', 'atombtc', 'getbtc', 'propybtc', 'irisbtc', 'adaeth', 'btmeth', 'hitusdt', 'engeth', 'meeteth', 'wprbtc', 'ontbtc', 'cdcbtc', 'ogousdt', 'mdsusdt', 'wavesusdt', 'xrpbtc', 'mtbtc', 'bcdbtc', 'blzbtc', 'maneth', 'quneth', 'nexobtc', 'gtbtc', 'ftibtc', 'ektusdt', 'zrxbtc', 'bkbtbtc', 'ekobtc', 'wavesbtc', 'gxcbtc', 'covabtc', 'toseth', 'dbcbtc', 'elfeth', 'icxbtc', 'sspbtc', 'swftcbtc', 'bifibtc', 'ethbtc', 'storjbtc', 'bftbtc', 'oneusdt', 'aeeth', 'cvceth', 'wtceth', 'eoshusd', 'payeth', 'pvtht', 'eosusdt', 'lxtusdt', 'bt1btc', 'newbtc', 'creht', 'xmxbtc', 'ckbusdt', 'docketh', 'hptusdt', 'mtlbtc', 'iostht', 'mdsbtc', 'zileth', 'npxseth', 'wiccbtc', 'hiteth', 'btmusdt', 'akroht', 'iosteth', 'gveeth', 'onebtc', 'emht', 'cvntbtc', 'ckbbtc', 'newusdt', 'gntbtc', 'eosbtc', 'chatbtc', 'etcbtc', 'engbtc', 'stkbtc', 'lskbtc', 'rtebtc', 'loombtc', 'sbtcbtc', 'qtumeth', 'trioeth', 'bixbtc', 'mtneth', 'xvgbtc', 'storjusdt', 'vidyht', 'srnbtc', 'skmusdt', 'veneth', 'mexeth', 'gaseth', 'vsysht', 'xzceth', 'cnnbtc', 'rsrht', 'xtzeth', 'pnteth', 'crousdt', 'itceth', 'bateth', 'zenbtc', 'fsnbtc', 'etchusd', 'mtxbtc', 'ncceth', 'qashbtc', 'rbtcbtc', 'fttbtc', 'ctxcbtc', 'kanusdt', 'egtbtc', 'nulsbtc', 'elfusdt', 'topcbtc', 'ltcusdt', 'wiccusdt', 'covaeth', 'smtbtc', 'elaeth', 'gtht', 'thetaeth', 'btttrx', 'ltchusd', 'ekoeth', 'dtabtc', 'letbtc', 'wtcusdt', 'nodeht', 'forbtc', 'hceth', 'gnxeth', 'kcashbtc', 'wpreth', 'gsceth', 'uipbtc', 'atpusdt', '18cbtc', 'aacbtc', 'bkbteth', 'blzeth', 'nulsusdt', 'rcneth', 'gtceth', 'omgusdt', 'lxtbtc', 'paxhusd', 'wxtht', 'ugasbtc', 'bchht', 'polybtc', 'btmbtc', 'soceth', 'hoteth', 'bhdusdt', 'atometh', 'letusdt', 'egcceth', 'atpbtc', 'waxpeth', 'ctxcusdt', 'fttusdt', 'lambbtc', 'xembtc', 'bhdbtc', 'ltcbtc', 'bcvbtc', 'tntbtc', 'embtc', 'nknusdt', 'zilbtc', 'wicceth', 'vidyusdt', 'pvtusdt', 'ardrbtc', 'ttusdt', 'dgbbtc', 'xlmeth', 'dashbtc', 'vetusdt', 'boxeth', 'kmdbtc', 'htbtc', 'waxpbtc', 'wtcbtc', 'swftceth', 'batusdt', 'ckbht', 'lambusdt', 'uipusdt', 'elausdt', 'xzcbtc', 'xemusdt', 'npxsbtc', 'smtusdt', 'adxeth', 'rsrbtc', 'dcreth', 'vsysbtc', 'kanbtc', 'loometh', 'trxusdt', 'elfbtc', 'mtnbtc', 'dbceth', 'omgbtc', 'linkusdt', 'lsketh', 'mxcbtc', 'musketh', 'manausdt', 'lxteth', 'uuueth', 'mxbtc', 'rsrusdt', 'skmht', 'socbtc', 'qspeth', 'trxbtc', 'gnteth', 'paibtc', 'gvebtc', 'srneth', 'dockbtc', 'usdchusd', 'evxbtc', 'hptht', 'mxusdt', 'paiusdt', 'qasheth', 'fsnusdt', 'gntusdt', 'topht', 'cvnteth', 'mtxeth', 'emusdt', 'xrpht', 'salteth', 'vetbtc', 'newht', 'rcccbtc', 'etcusdt', 'stketh', 'xzcusdt', 'ocneth', 'arpaht', 'leteth', 'smteth', 'vsysusdt', 'dockusdt', 'nccbtc', 'triobtc', 'nanobtc', 'zeneth', 'tnbeth', 'gscbtc', 'reqbtc', 'manabtc', 'lbaeth', 'portalbtc', 'lambeth', 'faireth', 'ugaseth', 'dashhusd', 'ethusdt', 'socusdt', 'croht', 'lolht', 'appcbtc', 'aaceth', 'xlmusdt', 'dashusdt', 'ethhusd', 'topusdt', 'wanbtc', 'batbtc', 'hcusdt', 'elabtc', 'fttht', 'bhtht', 'evxeth', 'zilusdt', 'htusdt', 'powrbtc', 'cnnsht', 'datxeth', 'hthusd', 'sncbtc', 'tusdhusd', 'phxbtc', 'linkbtc', 'cmteth', 'topbtc', 'uipeth', 'iicbtc', 'ektbtc', 'nknbtc', 'ttht', 'pvtbtc', 'algoeth', 'pceth', 'omgeth', 'luneth', 'adxbtc', 'iotabtc', 'zrxusdt', 'iotausdt', 'ocnbtc', 'sheeth', 'lbausdt', 'dgbeth', 'mxht', 'kaneth', 'aidocbtc', 'sntbtc', 'atomusdt', 'etnbtc', 'forht', 'xlmbtc', 'muskbtc', 'mtht', 'saltbtc', 'cmtusdt', 'gxcusdt', 'irisusdt', 'atpht', 'xrphusd', 'btsbtc', 'nknht', 'nanousdt', 'uuubtc', 'arpabtc', 'knceth', 'wxtbtc', 'xrpusdt', 'hotbtc', 'ardreth', 'polyeth', 'snceth', 'lambht', 'abteth', 'yeeeth', 'butbtc', 'hteth', 'kmdeth', 'arpausdt', 'bchhusd', 'cmtbtc', 'rcnbtc', 'asteth', 'bchusdt', 'ncasheth', 'nasusdt', 'qspbtc', 'dcrbtc', 'zecusdt', 'btsusdt', 'zechusd', 'bcxbtc', 'fsnht', 'paieth', '18ceth', 'uuuusdt', 'adabtc', 'bttusdt', 'dogeeth', 'ycceth', 'kcasheth', 'rccceth', 'egtht', 'steembtc', 'zlaeth', 'bt2btc', 'sntusdt', 'zecbtc', 'hcbtc', 'usdthusd', 'seeleusdt', 'ruffeth', 'nasbtc', 'bttbtc', 'seeleeth', 'reqeth', 'ocnusdt', 'lunbtc', 'veteth', 'kcashht', 'dateth', 'nanoeth', 'appceth', 'nodebtc', 'geteth', 'hb10usdt', 'zjltbtc', 'ucbtc', 'iotaeth', 'zrxeth', 'dcrusdt', 'ftieth', 'etcht', 'lymeth', 'xmrbtc', 'cvcusdt', 'crebtc', 'gxceth', 'iriseth', 'chateth', 'cnnsusdt', 'aeusdt', 'mcoeth', 'linketh', 'portaleth', 'waveseth', 'pntbtc', 'ttbtc', 'utkbtc', 'meetbtc', 'adausdt', 'fairbtc', 'actusdt', 'osteth', 'vidybtc', 'manaeth', 'cdceth', 'steemusdt', 'rdneth', 'qunbtc', 'mdseth', 'mteth', 'actbtc', 'bhtbtc', 'onteth', 'datxbtc', 'powreth', 'manbtc', 'trxeth', 'lolusdt', 'algobtc', 'lbabtc', 'propyeth', 'xmxeth', 'ogoht', 'waneth', 'tnbbtc', 'renbtc', 'bhtusdt', 'lolbtc', 'tosbtc', 'ekteth', 'dacbtc', 'creusdt', 'dashht', 'grseth', 'algousdt', 'boxbtc', 'cvcbtc', 'akrobtc', 'iiceth']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2017-10-23
      相关资源
      最近更新 更多