使用python请求Foursquare API时出现问题

2024-05-08 18:49:35 发布

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

我有一个pd数据框dfu数据与城市,纬度和经度的名称。 我尝试用我的凭证创建一个Foursquare请求,以查找位于这些城市周围特定半径内的所有商场。你知道吗

Malls\u 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    ```

Tags: nameinselfdfnewdatanameslocation