我正在尝试使用此代码从 foursquare 中抓取场地
def getNearbyVenues(names, latitudes, longitudes, radius=500):
venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
print(name)
# Create the API request URL
url = 'https://api.foursquare.com/v3/places/nearby&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
lat,
lng,
radius,
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)
但我收到此错误
{
"meta": {
"code": 410,
"errorType": "deprecated",
"errorDetail": "Usage of the V2 Places API has been deprecated for new Projects. Please see our updated documentation for V3 for more details: https://docs.foursquare.com/reference",
"requestId": "619928ff498a047e3a81f23a"
},
"response": {}
}
我应该在代码中编辑什么才能使其正常工作
我无法从此链接中弄清楚| ||https://developer.foursquare.com/reference/place-search 更新
使用@Corralien的解决方案: 这是 results.json() 的结果 我正在尝试获取地点地点纬度地点经度地点类别我必须编写更多内容,所以我的代码主要不是代码
This the result of results.json() I'm trying to get Venue Venue Latitude Venue Longitude Venue Category I have to write more so my code is not mostly code
'timezone': 'Europe/Helsinki'},
{'fsq_id': '4adcdb23f964a520c76021e3',
'categories': [{'id': 10031,
'name': 'Science Museum',
'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/arts_entertainment/museum_science_',
'suffix': '.png'}}],
'chains': [],
'distance': 148,
'geocodes': {'main': {'latitude': 60.171349882576,
'longitude': 24.93154852286545}},
'location': {'address': 'Pohjoinen Rautatiekatu 13',
'country': 'FI',
'cross_street': 'Arkadiankatu / Nervanderinkatu',
'locality': 'Helsinki',
'neighborhood': ['Etu-Töölö'],
'postcode': '00100',
提供的错误消息表明正在尝试使用 Foursquare Places API 的 V2 版本,该版本已被弃用。需要更新代码以使用 V3 版本。
以下是更新代码以使用 Foursquare Places API V3 的方法:
```python import requests import pandas as pd
CLIENT_ID = 'YOUR_CLIENT_ID' CLIENT_SECRET = 'YOUR_CLIENT_SECRET' VERSION = '20230801' LIMIT = 50
def getNearbyVenues(names, latitudes, longitudes, radius=500):
venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
print(name)
# 创建 API 请求 URL
url = f'https://api.foursquare.com/v3/places/search?ll={lat},{lng}&radius={radius}&limit={LIMIT}'
# 设置请求头
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {YOUR_API_KEY}"
}
# 发出 GET 请求
response = requests.get(url, headers=headers)
# 检查请求是否成功
if response.status_code == 200:
results = response.json()
else:
raise Exception(f"请求失败,状态码:{response.status_code}")
# 提取相关信息
for result in results['results']:
venue = {
'Neighborhood': name,
'Neighborhood Latitude': lat,
'Neighborhood Longitude': lng,
'Venue': result['name'],
'Venue Latitude': result['geocodes']['main']['latitude'],
'Venue Longitude': result['geocodes']['main']['longitude'],
'Venue Category': result['categories'][0]['name'] if result['categories'] else None
}
venues_list.append(venue)
nearby_venues = pd.DataFrame(venues_list)
return(nearby_venues)
```
更改说明:
-
API 端点:
将 API 端点更新为
https://api.foursquare.com/v3/places/search
。 -
API 密钥:
使用的实际 API 密钥替换
YOUR_API_KEY
。 -
参数:
更新参数以匹配 V3 API。删除了
client_id
和client_secret
,并添加了Authorization
标头,其中包含的 API 密钥。 - 响应处理: 根据 V3 API 响应结构更新了结果提取逻辑。
使用代码:
-
替换占位符:
将
YOUR_API_KEY
替换为的实际 Foursquare API 密钥。 -
提供数据:
将
names
、latitudes
和longitudes
变量替换为的实际数据列表。
此代码将返回一个 Pandas DataFrame,其中包含请求的每个位置附近的场馆列表,以及它们的名称、纬度、经度和类别。
请记住查看 Foursquare Places API V3 文档以获取有关可用端点、参数和响应格式的更多详细信息:
希望这有帮助!如果有任何其他问题,请告诉我。
标签:python,web-scraping From: 70904066