Bulk Posting on Facebook Groups using python .
Problem Statement :
- As we are aware that there are multiple tasks in the marketing agencies which are happening manually, and one of those tasks is bulk posting on several Facebook groups , which is very time-consuming and sometimes very tedious to do.
- In this project-based article, we are going to explore a solution based on python selenium library and see how we can use it & implement this to use it for our daily tasks.
Requirement :
- Python selenium library (This library is like API to access the selenium web driver to control Chrome, Firefox, etc.
- Chromium Web drivers (To use & Control Chrome browser, this driver needs to be present in your system)
Installations :
- To install selenium.
pip install selenium
- For web driver, you need to download the chromedriver.exe file and need to put it in the root directory .
Note: Also provided the chromedriver.exe with the repo. But it is better to download the latest driver from the official website to avoid the version issues
- Important Step : Initialization of Selenium and libraries used . We have to disable push notifications so that there is no blocking of our script.
Let’s explore the project files:
- PostingContents.txt: In this file, We will give the text content which we want to publish on Facebook Groups .
- fb_credentials.json: In this JSON file, we will enter these details:
- In the “Email Address” field, give your Facebook login email address, and this information is only to log in to Facebook.
- In the “Password” field, give your Facebook password to log in. This information is only to log in to Facebook.
- We are using this function to login to Facebook , here we are using find_element_by_id to find the buttons. (feel free to inspect)
- group_links.txt : In this txt file, you need to paste the URLs of the groups as comma separated URLs like link1, link2, link3 .
- URLs are of the form https:/www.facebook.com/id/…… , we will extract the ids which is an integer from the links while parsing in this function :
- In the folder img paste the images you want to attach under 512 x 512 format as Facebook recommends you can leave it empty if you want to post a text based post .
- We are pulling the images from the img directory if we get true from the user in src_bot method else we go for text post .
- It is worth reading about Xpath selectors which have been defined in the init method .
- Using Xpath selectors I am storing in fb_posting the following paths :
1. New Post : What’s on your mind?
2. Enter input : Entering Text
3. Post Button : To post
4. Attach media — images from the img directory
- In the end we make a new instance and call the methods to execute our functions
How to run the Bot ?
- After performing the above steps, run the fbPost.exe or
python fbPost.py
- Press “Y” in the terminal if you wish to upload text with images and “N” if you just want to upload text based post .
- That’s it! Sit back and watch the bot post in your Facebook groups.
- Clone or download the repository here
Output :
Full Code :
That’s it!
I hope you liked this project . Feel free give it a star and ask me any questions. ☺