对于大学的一个项目,我需要从相同的 Facebook 页面中提取数据,例如帖子和评论。几个月前一切都很好,但现在要从页面获取数据,您需要公共(public)页面内容访问权限。
为了让我的应用程序得到审核,我需要添加:
作为一个只需要为考试提取一些数据的学生,我没有任何可以使用该应用程序的网站/平台。我在 Python 上使用 Facebook Graph API。
我看了 this website对于隐私政策生成器,但我没有可以使用 API 的任何网站或移动应用程序...
我的情况是否有某种方法可以在没有此要求的情况下通过 API 提取数据,或者我最好找到其他解决方案,例如网页抓取?
最佳答案
为了能够使用 Python 代码从 Facebook 提取数据,您需要在 Facebook 上注册为开发人员,然后拥有访问 token 。这是它的步骤。
Go to link developers.facebook.com, create an account there. Go to link developers.facebook.com/tools/explorer. Go to “My apps” drop down in the top right corner and select “add a new app”. Choose a display name and a category and then “Create App ID”. Again get back to the same link developers.facebook.com/tools/explorer. You will see “Graph API Explorer” below “My Apps” in the top right corner. From “Graph API Explorer” drop down, select your app. Then, select “Get Token”. From this drop down, select “Get User Access Token”. Select permissions from the menu that appears and then select “Get Access Token.” Go to link developers.facebook.com/tools/accesstoken. Select “Debug” corresponding to “User Token”. Go to “Extend Token Access”. This will ensure that your token does not expire every two hours.
访问 Facebook 公共(public)数据的 Python 代码:
转至链接 https://developers.facebook.com/docs/graph-api如果想收集任何公开可用的数据。见 https://developers.facebook.com/docs/graph-api/reference/v2.7/ .从本文档中,选择您想要从中提取数据的任何字段,例如“组”或“页面”等。选择这些后转到代码示例,然后选择“facebook graph api”,您将获得有关提示如何提取信息。这个博客主要是关于获取事件数据。
首先,导入“urllib3”、“facebook”、“requests”(如果它们已经可用)。如果没有,请下载这些库。定义一个变量 token 并将其值设置为您在上面获得的“用户访问 token ”。
token= ‘aiufniqaefncqiuhfencioaeusKJBNfljabicnlkjshniuwnscslkjjndfi’
获取事件列表:
现在要查找任何搜索词的事件信息,请说“诗歌”并将这些事件的数量限制为 10000:
graph = facebook.GraphAPI(access_token=token, version = 2.7)
events = graph.request(‘/search?q=Poetry&type=event&limit=10000’)
这将给出在 Facebook 上创建的所有事件的字典,并在其名称中包含字符串“Poetry”。要获取事件列表,请执行以下操作:
eventList = events[‘data’]
从上面提取的事件列表中提取事件的所有信息:
获取列表中第一个事件的 EventID
eventid = eventList[1][‘id’]
对于此 EventID,获取所有信息并设置一些稍后将使用的变量:
event1=graph.get_object(id=eventid,fields=’attending_count,can_guests_invite,category,cover,declined_count,description,end_time,guest_list_enabled,interested_count,is_canceled,is_page_owned,is_viewer_admin,maybe_count,noreply_count,owner,parent_group,place,ticket_uri,timezone,type,updated_time’)
attenderscount = event1[‘attending_count’]
declinerscount = event1[‘declined_count’]
interestedcount = event1[‘interested_count’]
maybecount = event1[‘maybe_count’]
noreplycount = event1[‘noreply_count’]
获取所有参加事件的人的列表并将响应转换为 json 格式:
attenders = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/attending?
access_token="+token+”&limit=”+str(attenderscount))
attenders_json = attenders.json()
获取事件管理员:
admins = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/admins?
access_token="+token)
admins_json = admins.json()
同样,您可以根据需要提取其他信息,例如该事件的照片/视频/提要。
转至 https://developers.facebook.com/docs/graph-api/reference/event/并查看文档中的“边缘”部分。
关于python - 如何获取 Facebook 公共(public)页面内容访问权限只是为了提取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51221607/