gpt4 book ai didi

python - 如何在 Python 中编辑 Facebook 帖子?

转载 作者:行者123 更新时间:2023-12-04 16:18:56 28 4
gpt4 key购买 nike

我的 fb 页面上有一个帖子,我需要每天更新几次,其中包含在 Python 脚本中详细说明的数据。我尝试使用 Selenium,但是在保存帖子时它经常卡住,因此脚本也卡住了,所以我试图找到一种方法来在 python 本身中完成这项工作,而无需使用 Web 浏览器。
我想知道有没有办法使用诸如 Facepy 或类似的 Python 库来编辑 FB 帖子?
我正在阅读 graph API引用但是没有可以学习的例子,但我想第一件事是设置登录。在脸谱上 github页面写的是

note that Facepy does not do authentication with Facebook; it only consumes its API. To get an access token to consume the API on behalf of a user, use a suitable OAuth library for your platform


我尝试使用 BeautifulSoup 登录
from bs4 import BeautifulSoup
import requests
import re

def facebook_login(mail, pwd):
session = requests.Session()
r = session.get('https://www.facebook.com/', allow_redirects=False)
soup = BeautifulSoup(r.text)
action_url = soup.find('form', id='login_form')['action']
inputs = soup.find('form', id='login_form').findAll('input', {'type': ['hidden', 'submit']})
post_data = {input.get('name'): input.get('value') for input in inputs}
post_data['email'] = mail
post_data['pass'] = pwd.upper()
scripts = soup.findAll('script')
scripts_string = '/n/'.join([script.text for script in scripts])
datr_search = re.search('\["_js_datr","([^"]*)"', scripts_string, re.DOTALL)
if datr_search:
datr = datr_search.group(1)
cookies = {'_js_datr' : datr}
else:
return False
return session.post(action_url, data=post_data, cookies=cookies, allow_redirects=False)

facebook_login('email', 'psw')
但它给出了这个错误
action_url = soup.find('form', id='login_form')['action']
TypeError: 'NoneType' object is not subscriptable
我也试过 Mechanize
import mechanize

username = 'email'
password = 'psw'
url = 'http://facebook.com/login'

print("opening browser")
br = mechanize.Browser()
print("opening url...please wait")
br.open(url)
print(br.title())
print("selecting form")
br.select_form(name='Login')
br['UserID'] = username
br['PassPhrase'] = password
print("submitting form"
br.submit()
response = br.submit()
pageSource = response.read()
但它也给出了一个错误
mechanize._response.httperror_seek_wrapper: HTTP Error 403: b'request disallowed by robots.txt'

最佳答案

安装 facebook 包裹

pip install facebook-sdk
然后更新/编辑您页面上的帖子只需运行
import facebook

page_token = '...'
page_id = '...'
post_id = '...'
fb = facebook.GraphAPI(access_token = page_token, version="2.12")
fb.put_object(parent_object=page_id+'_'+post_id, connection_name='', message='new text')

关于python - 如何在 Python 中编辑 Facebook 帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65081429/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com