JSONDecodeError:预期值:第2行第1列(字符1)-Zomato API

时间:2020-06-07 08:18:33

标签: json api python-requests jsondecoder zomato-api

我正在尝试使用Zomato / Search API收集有关给定区域中餐馆的信息,并将其存储在“ zomato_venues”数据框中。

我有一个名为“ foursqaure_venues”的数据框,其中包含纬度,经度和其他详细信息。我已将纬度,经度和地点名称作为输入传递给Zomato / Search API,但它总是使我出错。

“ foursqaure_venues”数据框: enter image description here

代码:

headers = {
    'user-key': '<Zomato User-Key>'
}

venues_information = []
for i,j,k in zip(foursquare_venues['venue.name'],foursquare_venues['venue.location.lat'],foursquare_venues['venue.location.lng']):

    url=('https://developers.zomato.com/api/v2.1/search?q={}' + 
          '&start=0&lat={}&lon={}&radius=3000').format(i,j,k)
    response = requests.get(url, headers=headers).json()
    if len(response['restaurants'])>0:
        for i in range(len(response['restaurants'])):
            zom_venue=[]
            zom_venue.append(response['restaurants'][i]['restaurant']['name'])
            zom_venue.append(response['restaurants'][i]['restaurant']['location']['latitude'])
            zom_venue.append(response['restaurants'][i]['restaurant']['location']['longitude'])
            zom_venue.append(response['restaurants'][i]['restaurant']['average_cost_for_two'])
            zom_venue.append(response['restaurants'][i]['restaurant']['price_range'])
            zom_venue.append(response['restaurants'][i]['restaurant']['user_rating']['aggregate_rating'])
            zom_venue.append(response['restaurants'][i]['restaurant']['location']['address'])
            zom_venue.append(response['restaurants'][i]['restaurant']['cuisines'])
            zom_venue.append(response['restaurants'][i]['restaurant']['has_online_delivery'])
            zom_venue.append(response['restaurants'][i]['restaurant']['all_reviews_count'])
            zom_venue.append(response['restaurants'][i]['restaurant']['url'])
            venues_information.append(zom_venue)

    else:
        venues_information.append(np.zeros(6))

zomato_venues = pd.DataFrame(venues_information, 
                                     columns = ['venue', 'latitude', 
                                                'longitude', 'price_for_two', 
                                                 'price_range', 'rating', 'address','cuisines','online_delivery','review_count','url'])
zomato_venues.head()

错误:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-25-d8531aa48f6e> in <module>
      8     url=('https://developers.zomato.com/api/v2.1/search?q={}' + 
      9           '&start=0&lat={}&lon={}&radius=3000').format(i,j,k)
---> 10     response = requests.get(url, headers=headers).json()
     11     if len(response['restaurants'])>0:
     12         for i in range(len(response['restaurants'])):

c:\users\aasth\appdata\local\programs\python\python37-32\lib\site-packages\requests\models.py in json(self, **kwargs)
    896                     # used.
    897                     pass
--> 898         return complexjson.loads(self.text, **kwargs)
    899 
    900     @property

c:\users\aasth\appdata\local\programs\python\python37-32\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

c:\users\aasth\appdata\local\programs\python\python37-32\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

c:\users\aasth\appdata\local\programs\python\python37-32\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 2 column 1 (char 1)

我继续使用Zomato API文档,并使用/ Search API只是为了检查它是否正常工作以及是否正常工作: enter image description here

JSON示例:

{
  "results_found": 1488694,
  "results_start": 0,
  "results_shown": 20,
  "restaurants": [
    {
      "restaurant": {
        "R": {
          "has_menu_status": {
            "delivery": -1,
            "takeaway": -1
          },
          "res_id": 19328145,
          "is_grocery_store": false
        },
        "apikey": "<Zomato api key>",
        "id": "19328145",
        "name": "The Beirut Blends",
        "url": "https://www.zomato.com/thiruvalla/the-beirut-blends-thiruvalla-locality?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
        "location": {
          "address": "Kunnathuparambil Complex, T.K Road, Thiruvalla Locality, Thiruvalla",
          "locality": "Thiruvalla Locality",
          "city": "Thiruvalla",
          "city_id": 11798,
          "latitude": "9.3850460000",
          "longitude": "76.5769600000",
          "zipcode": "",
          "country_id": 1,
          "locality_verbose": "Thiruvalla Locality, Thiruvalla"
        },
        "switch_to_order_menu": 0,
        "cuisines": "Cafe, Arabian, BBQ, Continental",
        "timings": "11am – 11pm (Mon-Sun)",
        "average_cost_for_two": 300,
        "price_range": 1,
        "currency": "Rs.",
        "highlights": [
          "Dinner",
          "Delivery",
          "Credit Card",
          "Lunch",
          "Cash",
          "Takeaway Available",
          "Debit Card",
          "Indoor Seating",
          "Air Conditioned",
          "Digital Payments Accepted"
        ],
        "offers": [],
        "opentable_support": 0,
        "is_zomato_book_res": 0,
        "mezzo_provider": "OTHER",
        "is_book_form_web_view": 0,
        "book_form_web_view_url": "",
        "book_again_url": "",
        "thumb": "",
        "user_rating": {
          "aggregate_rating": "4.2",
          "rating_text": "Very Good",
          "rating_color": "5BA829",
          "rating_obj": {
            "title": {
              "text": "4.2"
            },
            "bg_color": {
              "type": "lime",
              "tint": "700"
            }
          },
          "votes": "704"
        },
        "all_reviews_count": 40,
        "photos_url": "https://www.zomato.com/thiruvalla/the-beirut-blends-thiruvalla-locality/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop",
        "photo_count": 47,
        "menu_url": "https://www.zomato.com/thiruvalla/the-beirut-blends-thiruvalla-locality/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop",
        "featured_image": "",
        "has_online_delivery": 1,
        "is_delivering_now": 0,
        "store_type": "",
        "include_bogo_offers": true,
        "deeplink": "zomato://restaurant/19328145",
        "is_table_reservation_supported": 0,
        "has_table_booking": 0,
        "events_url": "https://www.zomato.com/thiruvalla/the-beirut-blends-thiruvalla-locality/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
        "phone_numbers": "+91 7510731425, +91 8086994927",
        "all_reviews": {
          "reviews": [
            {
              "review": []
            },
            {
              "review": []
            },
            {
              "review": []
            },
            {
              "review": []
            },
            {
              "review": []
            }
          ]
        },
        "establishment": [
          "Quick Bites"
        ],
        "establishment_types": []
      }
    },

0 个答案:

没有答案