Building a Python Bot for Automated Weibo Login and Posting
Environment Setup and Prerequisites
Establishing a robust automation environment requires a functioning Python installation and the Selenium package. Install the necessary library using pip.
pip install seleniumAdditionally, the ChromeDriver executable must be downloaded and placed in a system-accessible directory. The driver version must strictly correspond to the installed version of the Google Chrome browser to ensure compatibility.
Browser Configuration
By default, Chrome may request notification permissions, which can interrupt automated flows. Configure browser options to suppress these pop-ups by modifying the user profile preferences.
options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
options.add_experimental_option("prefs", prefs)Element Location and Interaction
Web automation relies on accurately identifying Document Object Model (DOM) elements. Selenium provides strategies such as ID, name, CSS selectors, and XPath for locating elements. Common interactions include sending keystrokes to input fields, clearing existing content, and clicking buttons or links.
Implementing Direct Login
The following script demonstrates the workflow for logging in with credentials and posting a status update. It handles browser initialization, element interaction, and page transitions.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
import os
def automate_weibo_posting(account, pwd, message):
# Configure service and options
driver_path = os.path.join(os.getcwd(), 'chromedriver.exe')
service = Service(driver_path)
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
# Initialize browser
browser = webdriver.Chrome(service=service, options=chrome_options)
browser.maximize_window()
try:
# Navigate to login page
browser.get("https://weibo.com/login.php")
time.sleep(5)
# Locate inputs and enter credentials
username_field = browser.find_element(By.ID, "loginname")
password_field = browser.find_element(By.CSS_SELECTOR, "input[type='password']")
username_field.send_keys(account)
password_field.send_keys(pwd)
# Submit login form
login_button = browser.find_element(By.XPATH, "//div[@node-type='normal_form']//div[@class='info_list login_btn']/a")
login_button.click()
# Wait for manual verification (e.g., QR code) if required
time.sleep(40)
# Locate and populate the post textarea
post_area = browser.find_element(By.XPATH, '//*[@id="homeWrap"]/div[1]/div/div[1]/div/textarea')
post_area.send_keys(message)
# Click publish button
publish_btn = browser.find_element(By.XPATH, '//*[@id="homeWrap"]/div[1]/div/div[4]/div/button')
publish_btn.click()
time.sleep(10)
finally:
browser.quit()
if __name__ == "__main__":
automate_weibo_posting("your_username", "your_password", "Hello from Python automation")Persistent Authentication via Cookies
To avoid repetitive security checks like QR codes, a cookie-based approach is effective. This method involves saving session cookies after a manual login and reloading them in subsequent sessions to restore the session state.
import
def persist_cookies(driver_obj, file_path="weibo_cookies."):
"""Save current browser cookies to a file."""
current_cookies = driver_obj.get_cookies()
with open(file_path, "w") as f:
.dump(current_cookies, f)
print("Session cookies saved to disk.")
def load_session(driver_obj, file_path="weibo_cookies."):
"""Load cookies from file into the browser session."""
with open(file_path, "r") as f:
cookies = .load(f)
for cookie in cookies:
# Clean up keys that might cause issues when adding
cookie.pop('sameSite', None)
driver_obj.add_cookie(cookie)
driver_obj.refresh()
time.sleep(3)Verifying Cookie Validity
Before running automated tasks, it is prudent to verify if the stored cookies are still valid. This can be achieved by sending a request with the cookies and checking if the response contains user-specific data.
import requests
def validate_cookies(file_path="weibo_cookies."):
"""Check if cookies are still valid."""
with open(file_path, "r") as f:
cookies = .load(f)
session = requests.Session()
for cookie in cookies:
session.cookies.set(cookie['name'], cookie['value'])
response = session.get("https://weibo.com")
# Check for a specific username or indicator in the HTML
return "YourNickname" in response.textScheduling Daily Tasks
Integrating a scheduler allows for unattended, periodic posting. The `apscheduler` library can trigger the posting function at specific intervals.
from apscheduler.schedulers.blocking import BlockingScheduler
def daily_task():
# Initialize browser and log in using cookies
# ... (Browser setup and load_session code here) ...
# Example content generation
import requests
resp = requests.get("https://api.quotable.io/random")
content = resp.().get('content', 'Default text')
# Execute posting
# ... (Post automation code here) ...
print(f"Posted: {content}")
def start_scheduler():
scheduler = BlockingScheduler(timezone='Asia/Shanghai')
# Trigger every day at 9:00 AM
scheduler.add_job(daily_task, 'cron', hour=9)
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass
if __name__ == "__main__":
start_scheduler()