【问题标题】:How to extract values from a Python request如何从 Python 请求中提取值
【发布时间】:2016-01-16 03:18:28
【问题描述】:

我正在使用 Python 请求来搜索特定项目。

page = requests.get('https://utas.s2.fut.ea.com/ut/game/fifa16/transfermarket?maxb=900&start=0&type=player&maskedDefId=177604&num=16', headers=headers, cookies=cookies)
print page.json

上面的代码以 json 格式输出(见下文),我想只提取 tradeId 并将其打印到我的终端。

{u'sellerEstablished': 0, u'itemData': {u'rating': 80, u'itemType': u'player', u'resourceId': -2147306044, u'cardsubtypeid': 1, u'morale': 50, u'lifetimeAssists': 0, u'id': 242123805234, u'statsList': [{u'index': 0, u'value': 45}, {u'index': 1, u'value': 0}, {u'index': 2, u'value': 0}, {u'index': 3, u'value': 1}, {u'index': 4, u'value': 0}], u'assetId': 177604, u'teamid': 1, u'pile': 5, u'suspension': 0, u'rareflag': 1, u'playStyle': 250, u'attributeList': [{u'index': 0, u'value': 77}, {u'index': 1, u'value': 53}, {u'index': 2, u'value': 72}, {u'index': 3, u'value': 75}, {u'index': 4, u'value': 81}, {u'index': 5, u'value': 72}], u'loyaltyBonus': 1, u'timestamp': 1443853245, u'training': 0, u'nation': 45, u'assists': 0, u'formation': u'f442', u'lifetimeStats': [{u'index': 0, u'value': 45}, {u'index': 1, u'value': 0}, {u'index': 2, u'value': 0}, {u'index': 3, u'value': 1}, {u'index': 4, u'value': 1}], u'leagueId': 13, u'untradeable': False, u'discardValue': 640, u'owners': 2, u'injuryGames': 0, u'contract': 3, u'injuryType': u'none', u'lastSalePrice': 1500, u'fitness': 99, u'itemState': u'forSale', u'preferredPosition': u'LB'}, u'tradeId': 159804881070, u'confidenceValue': 100, u'sellerName': u'FIFA UT', u'expires': 1745, u'buyNowPrice': 900, u'tradeOwner': False, u'offers': 0, u'startingBid': 650, u'watched': None, u'currentBid': 700, u'tradeState': u'active', u'bidState': u'none', u'sellerId': 0}

【问题讨论】:

    标签: python json parsing python-requests


    【解决方案1】:

    将其用作字典。

    a = page.json()
    print a['tradeId']
    

    【讨论】:

    • 他们使用的是较旧的 requests 版本(可能在 Ubuntu LTS 打包的 0.8 系列中),其中 .json 仍然是属性而不是方法。
    • 但是操作提到的输出看起来像字典。所以如果可以识别page.json的类型,我们就可以知道如何访问tradeId。我可能错了,但值得一试:)
    • 当然,但是您将 page.json 引用更改为 page.json(),这可能会导致 OP 出错。
    • 收到以下错误'instancemethod'对象没有属性'getitem'
    • 你能检查一下a的类型是什么,即打印类型(a)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多