Scrapy数据选择

时间:2014-10-21 19:03:54

标签: python scrapy

我想废弃此纯文本中的数据:

 "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

1 个答案:

答案 0 :(得分:0)

这不是一个纯文本!这是一个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