|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如果您经常在 Facebook 上发帖,提高工作效率的最佳方法之一就是使用自动发帖调度程序。一种方法是创建一个简单的 Python 脚本,您可以将其附加到日历或 cron 作业。现在让我们看看创建该脚本。
One way to improve productivity if you regularly post to Facebook is with an automatic post scheduler. One way to do that is to create a simple Python script that you can attach to a calendar or cron job. Let’s look at creating that script now.
如果您经常在 Facebook 上发帖,那么提高工作效率的一种方法是使用自动发帖调度程序。一种方法是创建一个简单的 Python 脚本,您可以将其附加到日历或 cron 作业。现在让我们看看创建该脚本。
## Creating a Facebook app
## 创建 Facebook 应用程序
Any Python script that we create to post on Facebook will use a Facebook app to do so, and you will need to make it before we can start with the Python script. Luckily, it’s not hard.
我们创建的要在 Facebook 上发布的任何 Python 脚本都将使用 Facebook 应用程序来执行此操作,您需要先完成它,然后才能开始使用 Python 脚本。幸运的是,这并不难。
1. Go to the Meta Developers site and log in.
1. 转至 Meta Developers 站点并登录。
2. Click on “My Apps” in the top navigation bar.
2. 单击顶部导航栏中的“我的应用程序”。
3. Click on “Create New App.”
3. 单击“创建新应用程序”。
4. Select “Manage Page” as the app type and click on “Continue.”
4. 选择“管理页面”作为应用程序类型,然后单击“继续”。
5. Enter a display name for your app and click on “Create App.”
5. 输入应用程序的显示名称,然后单击“创建应用程序”。
6. Click on “Skip Quick Start” and then on “Settings.”
6. 单击“跳过快速启动”,然后单击“设置”。
7. In the left sidebar, click on “Basic Settings.”
7. 在左侧边栏中,单击“基本设置”。
8. Under “App Domains,” enter the domain name of the website or server that will be hosting your Python script. For example, if your script will be hosted on your personal website at the URL “https://example.com/script.py,” then you would enter “example.com” here.
8. 在“应用程序域”下,输入将托管 Python 脚本的网站或服务器的域名。例如,如果您的脚本将托管在您的个人网站上,URL 为“https://example.com/script.py”,那么您应在此处输入“example.com”。
9. Click on “Save Changes.”
9. 单击“保存更改”。
10. Next, we need to add our Facebook page to the app. In the left sidebar, click on “Pages.”
10. 接下来,我们需要将 Facebook 页面添加到应用程序中。在左侧边栏中,单击“页面”。
11. Click on the “Add Page” button and select the page that you want to give your app permission to post on.
11. 单击“添加页面”按钮,然后选择您想要授予应用程序发布权限的页面。
12. Click on the “Assign” button and then on the “Done” button.
12. 单击“分配”按钮,然后单击“完成”按钮。
13. Finally, we need to generate an access token for our app. In the left sidebar, click on “App Tokens.”
13. 最后,我们需要为我们的应用程序生成访问令牌。在左侧边栏中,单击“应用程序令牌”。
14. Click on the “Generate New Token” button and select “User Access Token” from the dropdown menu.
14. 单击“生成新令牌”按钮,然后从下拉菜单中选择“用户访问令牌”。
15. Click on the “Generate” button and copy the access token that is displayed. You will need this token later when we configure our Python script.
15. 单击“生成”按钮并复制显示的访问令牌。稍后当我们配置 Python 脚本时,您将需要此令牌。
## Get your page ID
## 获取你的页面ID
With the app created, we can use it as a go-between between Python and Facebook, but to make a post, we will need our page ID.
创建应用程序后,我们可以将其用作 Python 和 Facebook 之间的中间人,但要发布帖子,我们需要页面 ID。
1. Go to the Facebook page that you want to post on.
1. 转至您要发帖的 Facebook 页面。
2. In the URL address bar, look for the string of numbers that appears after “facebook.com/.” This is your page ID. For example, if the URL of your page is “https://www.facebook.com/geeksides/,” then your page ID is “geeksides.”
2. 在 URL 地址栏中,查找“facebook.com/”后面出现的数字字符串。这是您的页面 ID。例如,如果您的页面 URL 是“https://www.facebook.com/geeksides/”,那么您的页面 ID 就是“geeksides”。
## Get your page access token
## 获取您的页面访问令牌
The final thing we need before we can write our Python script is our page access token.
在编写 Python 脚本之前,我们需要的最后一件事是页面访问令牌。
1. Go to the Graph API Explorer site and log in.
1. 转到 Graph API Explorer 站点并登录。
2. In the left sidebar, click on “User Access Token.”
2. 在左侧边栏中,单击“用户访问令牌”。
3. Select the page that you want to get the access token for from the dropdown menu.
3. 从下拉菜单中选择您想要获取访问令牌的页面。
4. Click on the “Generate Access Token” button and copy the access token that is displayed. You will need this token later when we configure our Python script.
4. 单击“生成访问令牌”按钮并复制显示的访问令牌。稍后当我们配置 Python 脚本时,您将需要此令牌。
## The Python Script
## Python 脚本
If you don’t know how to run this script and are just getting started with Python, check out our long list of tutorials.
如果您不知道如何运行此脚本并且刚刚开始使用 Python,请查看我们的一长串教程。
You can copy and paste the following Python script into a text file and run it to make a post on the Facebook page you have the access code for:
您可以将以下 Python 脚本复制并粘贴到文本文件中,然后运行它以在您拥有访问代码的 Facebook 页面上发帖:
```python
````蟒蛇
# First, install the Requests if you don't already have it
# 首先,如果您还没有安装 Requests,请安装它
pip install requests
pip 安装请求
# Here is the complete code:
# 这是完整的代码:
import requests
导入请求
# Replace these with your actual access token and page ID or user ID
# 将这些替换为您的实际访问令牌和页面 ID 或用户 ID
access_token = 'your_facebook_access_token'
access_token = 'your_facebook_access_token'
page_id = 'your_page_or_user_id'
page_id = 'your_page_or_user_id'
message = 'Hello, this is a test post from my Python script!'
message = '您好,这是我的 Python 脚本的测试帖子!'
# Define the URL for posting to the Facebook Graph API
# 定义发布到 Facebook Graph API 的 URL
post_url = f'https://graph.facebook.com/v17.0/{page_id}/feed'
post_url = f'https://graph.facebook.com/v17.0/{page_id}/feed'
# Parameters to be sent with the request
# 与请求一起发送的参数
payload = {
有效负载={
'message': message,
'消息':消息,
'access_token': access_token
'access_token':access_token
}
}
# Make the POST request to Facebook
# 向 Facebook 发出 POST 请求
response = requests.post(post_url, data=payload)
响应 = requests.post(post_url, data=payload)
# Check if the post was successful
# 检查帖子是否成功
if response.status_code == 200:
如果响应.status_code == 200:
print('Post published successfully!')
print('帖子发布成功!')
else:
别的:
print(f'Failed to post. Status code: {response.status_code}')
print(f'发布失败。状态码:{response.status_code}')
print(f'Error: {response.text}')
print(f'错误:{response.text}')
```
````
To run this script, first change the code to include your page ID and access code where it says to do so. Then, save the file with a .py extension (e.g.,
要运行此脚本,请首先更改代码以包含您的页面 ID 和访问代码(在其指定的位置)。然后,使用 .py 扩展名保存文件(例如,
免责声明:info@kdj.com
所提供的信息并非交易建议。根据本文提供的信息进行的任何投资,kdj.com不承担任何责任。加密货币具有高波动性,强烈建议您深入研究后,谨慎投资!
如您认为本网站上使用的内容侵犯了您的版权,请立即联系我们(info@kdj.com),我们将及时删除。
-
- 为什么比特币价格跌破 60,000 美元
- 2024-10-12 06:15:02
- 美国CPI通胀数据公布后,比特币价格跌破6万美元。 CPI显示美国9月通胀达2.4%