【发布时间】:2012-04-02 20:53:05
【问题描述】:
我正在使用requests library 尝试在 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,但那里似乎出了点问题。
有谁知道最初可能导致此失败的原因以及如何解决它?
【问题讨论】:
标签: python redirect http-status-code-302 http-request python-requests