bbs_maowotao.py
基于 Selenium 的论坛抓取脚本,用显式等待控制翻页节奏,逐个页面取帖子数据。文件名对应具体目标站点,改一下选择器和翻页规则就能换到别的论坛,适合需要账号登录的社区内容收集。
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
# 创建WebDriver
driver = webdriver.Chrome()
# 打开需要登录的URL
driver.get("https://www.maowotao.com/")
# 等待用户手动完成登录
input("请完成登录后按回车键继续...")
# 定义URL链接列表
urls = [
"https://www.maowotao.com/home.php?mod=space&uid=152195&do=thread&view=me&from=space",
]
for url in urls:
try:
driver.get(url)
driver.execute_script('''
document.querySelectorAll('[id^="a_friend_li_"]').forEach(function(element) {
// 处理每个匹配的元素
element.click();
});
''')
time.sleep(5)
# Execute JavaScript to set the message in the input field
message = "https: //rebrand.ly/nwmql65 科学上网"
driver.execute_script("document.querySelector('input[name=note]').value = arguments[0];", message)
time.sleep(5)
# Click the submit button
driver.execute_script("document.getElementById('addsubmit_btn').click();")
time.sleep(5)
except Exception as e:
print("An error occurred: ", e)
continue