如何在 Python 中编辑 facebook post?
How to edit a facebook post in Python?
我的 fb 页面上有一个 post,我需要每天更新几次 python 脚本中详细说明的数据。我尝试使用 Selenium,但是在保存 post 时它经常卡住,因此脚本也卡住了,所以我试图找到一种方法来在 python 本身内完成这项工作而不使用网络浏览器.
我想知道有没有办法使用 python 库(例如 Facepy 或类似库)来编辑 FB post?
我正在阅读graph API reference but there are no examples to learn from, but I guess first thing is to set up the login. On the facepy 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
然后 update/edit 一个 post 在你的页面上只是 运行
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')
我的 fb 页面上有一个 post,我需要每天更新几次 python 脚本中详细说明的数据。我尝试使用 Selenium,但是在保存 post 时它经常卡住,因此脚本也卡住了,所以我试图找到一种方法来在 python 本身内完成这项工作而不使用网络浏览器.
我想知道有没有办法使用 python 库(例如 Facepy 或类似库)来编辑 FB post?
我正在阅读graph API reference but there are no examples to learn from, but I guess first thing is to set up the login. On the facepy 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
然后 update/edit 一个 post 在你的页面上只是 运行
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')