首页 > 解决方案 > 检索 Instagram 标记帖子的所有响应页面时出错

问题描述

我正在尝试获取标记了业务页面的所有 Instagram 帖子的数据。响应是一组 25 个分页对象。每个对象都有几个页面(下一页的端点在'next'键中)。能够检索前 2 个对象的数据(所有页面)。之后我得到: {'error': {'message': '发生了意外错误。请稍后重试您的请求。', 'type': 'OAuthException', 'is_transient': True, 'code': 2, 'fbtrace_id': 'AVCL6iRv2sQ80t87zS0Nysa'}}

我认为我没有超出任何速率限制

import requests
from pprint import pprint
import pandas as pd

#update this with the latest access token
temp_access_token = "[REDACTED]"

#replaced client_secret and client_id with xxxx
long_lived_access_token = requests.get("https://graph.facebook.com/v11.0/oauth/access_token?grant_type=fb_exchange_token&client_id=xxxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&fb_exchange_token="+temp_access_token)

long_lived_access_token = long_lived_access_token.json()['access_token']

#replaced business account id with xxxx
response = requests.get("https://graph.facebook.com/v11.0/xxxxxxxxxxxxxxxxx?fields=instagram_business_accounts%7Btags%7Bid%2Ccaption%2Ccomments_count%2Clike_count%2Ctimestamp%2Cusername%2Cmedia_product_type%2Cowner%2Cmedia_type%2Cpermalink%7D%7D&access_token="+long_lived_access_token)

response_obj = response.json()

response_obj = response_obj['instagram_business_accounts']['data']


data_list = []
for idx, obj in enumerate(response_obj):
    
    data_list+=obj['tags']['data']
    if obj['tags']['paging']['next']:
        temp_obj = requests.get(obj['tags']['paging']['next']).json()
        
        while True:
            if 'paging' not in temp_obj:
                data_list+=temp_obj['data']
                break
            data_list+=temp_obj['data']
            #next_set.add((temp_obj['paging']['next']))
            temp_obj = requests.get(temp_obj['paging']['next']).json()
    print(idx)

标签: facebookweb-scrapingfacebook-graph-apipython-requestsget

解决方案


推荐阅读