Board logo

Title: python design auto post program [Print this page]

Author: sky999    Time: 2024-8-6 10:46     Title: python design auto post program

import requests from bs4 import BeautifulSoup # 配置 forum_url = "http://your-discuz-forum.com" # 论坛的URL login_url = f"{forum_url}/logging.php?action=login" # 登录URL post_url = f"{forum_url}/post.php?action=newthread&fid=2" # 发帖URL,fid=2表示论坛板块ID username = "your_username" password = "your_password" post_title = "自动发帖标题" post_content = "这是自动发帖的内容。" # 登录函数 def login(session): login_data = { "username": username, "password": password, "loginsubmit": "true", "formhash": get_formhash(session, login_url) } response = session.post(login_url, data=login_data) print(f"登录状态码: {response.status_code}") print(f"登录响应: {response.text[:200]}...") # 只打印前200个字符 if "欢迎您回来" in response.text: print("登录成功") return True else: print("登录失败") return False # 获取formhash函数 def get_formhash(session, url): response = session.get(url) print(f"获取formhash状态码: {response.status_code}") soup = BeautifulSoup(response.text, "html.parser") formhash_input = soup.find("input", {"name": "formhash"}) if formhash_input: formhash = formhash_input["value"] print(f"获取formhash: {formhash}") return formhash else: print("未找到formhash") return None # 发帖函数 def post_thread(session): response = session.get(post_url) soup = BeautifulSoup(response.text, "html.parser") formhash = soup.find("input", {"name": "formhash"})["value"] post_data = { "subject": post_title, "message": post_content, "topicsubmit": "yes", "formhash": formhash } # 检查并添加所有隐藏字段 hidden_inputs = soup.find_all("input", type="hidden") for hidden_input in hidden_inputs: if hidden_input["name"] not in post_data: post_data[hidden_input["name"]] = hidden_input["value"] response = session.post(post_url, data=post_data) print(f"发帖状态码: {response.status_code}") print(f"发帖响应: {response.text[:200]}...") # 只打印前200个字符 if "帖子已发布" in response.text: print("发帖成功") else: print(f"发帖失败\n发帖响应: {response.text}") # 打印完整响应 # 主函数 def main(): with requests.Session() as session: if login(session): post_thread(session) if __name__ == "__main__": main()




Welcome AbyssalSwamp (http://iot.caffz123.uk:12345/mud/AbyssalSwamp/index/) caffz.com