我正在使用requests 库尝试在 phpBB 论坛上发帖。
我的代码如下所示:
self.r = requests.session()
d = self.r.get('http://examplephpbbforum.com/community/posting.php?mode=reply&f=' + forumid + '&t=' + topicid)
sid = stringBetween('style.php?sid=', '&id=2', d.content)
lastclick = stringBetween('lastclick" value="', '" />', d.content)
creation_time = stringBetween('creation_time" value="', '" />', d.content)
form_token = stringBetween('form_token" value="', '" />', d.content)
topic_cur_post_id = stringBetween('topic_cur_post_id" value="', '" />', d.content)
payload = {'addbbcode20':'100', 'message':message, 'topic_cur_post_id':topic_cur_post_id, 'lastclick':lastclick, 'post':'Submit', 'attach_sig':'on', 'creation_time':creation_time, 'form_token':form_token}
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1', 'Referer':'http://examplephpbbforum.com/community/posting.php?mode=reply&f=' + forumid + '&t=' + topicid, 'Content-Type':'application/x-www-form-urlencoded', 'Connection':'Keep-alive', 'Host':'examplephpbbforum.com', 'Accept-Language':'en-us,en;q=0.5', 'Accept-Encoding':'gzip, deflate', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}
d = self.r.post('http://examplephpbbforum.com/community/posting.php?mode=reply&f=' + forumid + '&sid=' + sid + '&t=' + topicid, data=payload, headers=headers, allow_redirects=True)
然后我通过在响应中搜索字符串来检查主题是否发布成功。
流量的流向是,在第一次发布之后,有一个 302 重定向,然后请求跟随到它成功发布到的页面。
有时它会起作用(可能是 50 次中的 1 次),但通常不起作用。响应只是第一个获取请求的页面。(发布回复页面)
它几乎每次都应该重定向到发布的消息,但由于某种原因它不会。
如果我用wireshark 嗅探流量,似乎响应通常会发送一个要重定向到的URL,但那里似乎出了点问题。
任何人都知道是什么可能导致这首先失败以及如何解决它?