Scrapy json响应:通配符和抓取引用

时间:2017-08-27 10:02:40

标签: python json scrapy

我正在尝试用Scrapy抓取JSON响应。我想知道是否有可能在JSON中刮取通配符路径,找到嵌套值" Metro"并拉动距离"在那个层次结构中。

在JSON中,有几个poi对象,但我只对Metro一对,以及到Metro的距离感兴趣。请参阅下面的我试图抓的例子。

我尝试使用以下代码,但它不起作用,因为通配符不起作用且引用不正确。我习惯于XPATH抓,所以希望有一些简单的方法来做到这一点?

loader.add_value('Metro', jsonresponse["poi"][*][["name"]== "Metro"]["distance"])

完整的JSON:

"poi":[
      {
         "distance":1469.0,
         "description":"Station",
         "walkDistance":1948,
         "url":"",
         "lon":14,
         "time":1890,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/263",
         "name":"Metro"
      },
      {
         "distance":2163.0,
         "description":"Station",
         "walkDistance":4371,
         "url":"",
         "lon":14,
         "time":4200,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/155",
         "name":"Tram"
      },
      {
         "distance":33.0,
         "description":"Station",
         "walkDistance":40,
         "url":"",
         "lon":14,
         "time":36,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/198",
         "name":"Bus MHD"
      },
      {
         "distance":1413.0,
         "description":"Station",
         "walkDistance":2615,
         "url":"",
         "lon":14,
         "time":2382,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/169",
         "name":"Vlak"
      },
      {
         "distance":487.0,
         "description":"Bankomat",
         "walkDistance":968,
         "url":"url",
         "lon":14,
         "time":943,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/28",
         "name":"Bankomat"
      },
      {
         "distance":473.0,
         "description":"Station",
         "walkDistance":614,
         "url":"url",
         "lon":14,
         "time":574,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/122",
         "name":"Police"
      },
      {
         "distance":188.0,
         "description":"Station",
         "walkDistance":250,
         "url":"url",
         "lon":14,
         "time":253,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/72",
         "name":"Apothecary"
      },
      {
         "distance":286.0,
         "description":"Station",
         "walkDistance":400,
         "url":"url",
         "lon":14,
         "time":381,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/144",
         "name":"Sport"
      },
      {
         "distance":286.0,
         "description":"Station",
         "walkDistance":400,
         "url":"url",
         "lon":14,
         "time":381,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/133",
         "name":"Restaurant"
      },
      {
         "distance":64.0,
         "description":"Station",
         "walkDistance":233,
         "url":"url",
         "lon":14,
         "time":216,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/423",
         "name":"Supermarket"
      },
      {
         "distance":168.0,
         "description":"Station",
         "walkDistance":320,
         "url":"url",
         "lon":14,
         "time":295,
         "lat":50,
         "imgUrl":"https://api.mapy.cz/poiimg/icon/142",
         "name":"School"
      }

1 个答案:

答案 0 :(得分:0)

如果你想要一个单行,那么你可以使用它:

distance = [x['distance'] for x in jsonresponse['poi'] if x['name'] == 'Metro'][0]