AbyssalSwamp  ActivaUser
» Guest:  Register | Login | 会员列表

RSS subscription to this AbyssalSwamp  

Previous thread Next thread
       
Title: python design auto post program  
 
sky999
天山茗客



UID 181291
Digest 2
Points 10
Posts 3937
码币MB 2542 Code
黄金 0 Catty
钻石 884 Pellet
Permissions 10
Register 2020-11-28
Status offline
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()



CAFFZ.com
2024-8-6 10:46#1
View profile  Blog  Send a short message  Top
       


  Printable version | Recommend to a friend | Subscribe to topic | Favorite topic  


 


All times are GMT+8, and the current time is 2026-1-24 02:29 Clear informations ->sessions/cookies - Contact Us - CAFFZ - ZAKE