Facebook Graph API中的分页(Paging)机制允许开发者分批获取大量数据。当请求返回的数据量较大时,API会返回一个分页对象,其中包含"next"和"previous"链接,用于获取下一页或上一页的数据。
import requests
def fetch_paginated_data(access_token, initial_url):
url = initial_url
all_data = []
while url:
try:
response = requests.get(url, params={'access_token': access_token})
data = response.json()
if 'error' in data:
print(f"Error: {data['error']['message']}")
break
all_data.extend(data['data'])
# 获取下一页链接
url = data.get('paging', {}).get('next', None)
# 避免频繁请求
time.sleep(1)
except Exception as e:
print(f"Request failed: {str(e)}")
break
return all_data
# 使用示例
access_token = "YOUR_ACCESS_TOKEN"
initial_url = "https://graph.facebook.com/v12.0/me/feed"
data = fetch_paginated_data(access_token, initial_url)
如果问题仍然存在,建议检查Facebook开发者平台的当前状态页面,确认是否有已知的API问题。
没有搜到相关的文章