1

我正在尝试使用请求来将消息发布到 phpBB3 论坛。这是一个更大项目的一部分,这部分应该用来自数据库的几千个帖子填充论坛。遍历论坛时,我还使用lxml来解析页面。

我的问题实际上是在论坛上发帖。首先我登录:

payload = {'username':'user', 'password':'password', 'login':'login', 
            'redirect':'index.php''}
url = 'http://test.com/test_forum'
q = s.post('http://test.com/test_forum/ucp.php?mode=login', data=payload)
a = s.get('http://test.com/test_forum')

在此之后,我可以看到我的脚本已登录。然后我使用 lxml 遍历论坛到我想要发布的主题/线程,并使用请求来获取发布页面,之后我再次使用 lxml 来获取<input>值从页面并将它们放入字典中。然后我尝试以与登录时类似的方式发布它,但没有任何反应:

#This is after traversing the forum using lxml to get to the post page.
a = s.get('%s%s' % (url, links[0][1:]))
#a contains the response (posting page)

tree = html.fromstring(a.text)
#collect values from inputs in the form to submit along with message
form_token = tree.xpath('//input[@name="form_token"]/attribute::value')
subject = tree.xpath('//input[@name="subject"]/attribute::value')
topic_cur_post_id = tree.xpath('//input[@name="topic_cur_post_id"]/attribute::value')
lastclick = tree.xpath('//input[@name="lastclick"]/attribute::value')
creation_time = tree.xpath('//input[@name="creation_time"]/attribute::value')
fileupload = tree.xpath('//input[@name="fileupload"]/attribute::value')

data = {'form_token':form_token, 'subject':subject, 
'topic_cur_post_id':topic_cur_post_id, 'lastclick':lastclick, 
'creation_time':creation_time,
        'fileupload':fileupload, 'message':'TEST MESSAGE 2'}

q = s.post('http://vaultlog.frih.net/test_forum/posting.php?mode=reply&f=9&t=4',
             data=data)

当我检查使用哪个页面加载print q.text时,我再次看到它是同一个发布页面。

我冒昧地使用 WireShark 来查看数据包的不同之处。首先,我脚本的 POST 消息: http: //pastebin.com/sRp9Zxme

当我使用 Firefox 发布消息时生成的消息:http: //pastebin.com/QSNawBRM

似乎主要区别在于自然发布消息会给 POST 一个content-type: multipart/form-data;标题,而我的脚本的标题是Content-Type: application/x-www-form-urlencoded.

想到的另一个想法是我没有发布所有必填字段。我没有提交任何复选框等,但我提交了所有字段,包括隐藏的字段。这是表单域的 HTML:http: //pastebin.com/U12BXZWF

为了使用请求发布到论坛,我必须做什么?如果这有任何改变,我还必须将图像作为附件发布。

4

0 回答 0