用python向Foursquare API请求的问题

时间:2019-04-02 18:00:10

标签: python foursquare

我有一个pd数据框df_data,其中包含城市名称,纬度和经度。 我尝试用我的凭据创建一个Foursquare请求,以查找那些城市周围特定半径内的所有购物中心。

Malls_id是购物中心的Foursquare定义ID。

我的请求出错。

我认为该错误来自添加categoryId,但我没有发现问题。

我在做什么错? 谢谢

def getNearbyVenues(names, latitudes, longitudes, radius=500, LIMIT=5):

venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
    print(name)

    # create the API request URL
    url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&categoryId={}&limit={}'.format(
        CLIENT_ID, 
        CLIENT_SECRET, 
        VERSION, 
        lat, 
        lng, 
        radius, 
        Malls_id,
        LIMIT)

    # make the GET request
    results = requests.get(url).json()["response"]['groups'][0]['items']

    # return only relevant information for each nearby venue
    venues_list.append([(
        name, 
        lat, 
        lng, 
        v['venue']['name'], 
        v['venue']['location']['lat'], 
        v['venue']['location']['lng'],  
        v['venue']['categories'][0]['name']) for v in results])

nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
nearby_venues.columns = ['Neighborhood', 
              'Neighborhood Latitude', 
              'Neighborhood Longitude', 
              'Venue', 
              'Venue Latitude', 
              'Venue Longitude', 
              'Venue Category']

return(nearby_venues)

# Run the above function on each location and create a new dataframe called location_venues and display it.
location_venues = getNearbyVenues(names=df_data['City'],
                               latitudes=df_data['Latitude'],
                               longitudes=df_data['Longitude']
                              )

它运行在我所有的位置

Warsaw
Carmel
Chesterton
Granger
Plainfield

,然后停止。 这是完整的堆栈跟踪:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-537f5e1b4f8a> in <module>()
     2 location_venues = getNearbyVenues(names=df_data['City'],
     3                                    latitudes=df_data['Latitude'],
----> 4                                    longitudes=df_data['Longitude']
     5                                   )

<ipython-input-20-23c3db7fc2a3> in getNearbyVenues(names, latitudes, longitudes, radius, LIMIT)
    36                   'Venue Latitude',
    37                   'Venue Longitude',
---> 38                   'Venue Category']
    39 
    40     return(nearby_venues)

/opt/conda/envs/DSX-Python35/lib/python3.5/site-packages/pandas/core/generic.py in __setattr__(self, name, value)
  3625         try:
  3626             object.__getattribute__(self, name)
-> 3627             return object.__setattr__(self, name, value)
  3628         except AttributeError:
  3629             pass

pandas/_libs/properties.pyx in pandas._libs.properties.AxisProperty.__set__()

/opt/conda/envs/DSX-Python35/lib/python3.5/site-packages/pandas/core/generic.py in _set_axis(self, axis, labels)
   557 
   558     def _set_axis(self, axis, labels):
--> 559         self._data.set_axis(axis, labels)
   560         self._clear_item_cache()
   561 

/opt/conda/envs/DSX-Python35/lib/python3.5/site-packages/pandas/core/internals.py in set_axis(self, axis, new_labels)
  3067             raise ValueError('Length mismatch: Expected axis has %d elements, '
  3068                              'new values have %d elements' %
-> 3069                              (old_len, new_len))
  3070 
  3071         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 0 elements, new values have 7 elements    ```

0 个答案:

没有答案