碎片数据选择

2024-09-26 17:41:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从纯文本中删除数据:

 "data": [
      {
         "id": "10150635906994798_21377910",
         "from": {
            "id": "100001249878256",
            "location" : "Stockholm"
            "name": "Mouhamadoul Moussa"
         },
         "message": "#Yeaaaahh!!! \u2665",

      },
      {
         "id": "10150635906994798_21392047",
         "from": {
             "id": "100000648164454",
              "location" : "Malmo"
            "name": "mallow ty"
         },
         "message": "droit au butttttttttttttttttt",
      },
    ]

但我只想检索第二个id,用于id选择的xpath response.selector.xpath ('//*[contains(text(), "id")]')

输出应为:

 100000648164454
 100001249878256

Tags: 数据namefrom文本idmessagedatalocation
1条回答
网友
1楼 · 发布于 2024-09-26 17:41:57

那不是纯文本!这是一个json。但是,您可以将其存储为字典:

>>> a = {'data': [{'from': {'id': '100001249878256',
...     'location': 'Stockholm',
...     'name': 'Mouhamadoul Moussa'},
...    'id': '10150635906994798_21377910',
...    'message': '#Yeaaaahh!!! \\u2665'},
...   {'from': {'id': '100000648164454', 'location': 'Malmo', 'name': 'mallow ty'},
...    'id': '10150635906994798_21392047',
...    'message': 'droit au butttttttttttttttttt'}]}

>>> for data in a['data']:
...     print data['from']['id']
... 
100001249878256
100000648164454

相关问题 更多 >

    热门问题