[使用请求库(python)发布到论坛

问题描述 投票:1回答:1

我正在尝试使用Requests以便将消息发布到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

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

python forms post python-requests phpbb3
1个回答
0
投票

为了学习,首先要了解所有python内置函数,并开始解决基本问题,例如加法,减法以及回文,二进制搜索,排序程序等基本编程,

然后选择诸如Django,flask或pyramid之类的任何Web框架开始构建基本应用程序,它将为您提供有关如何提高python编码知识的基本概念。

© www.soinside.com 2019 - 2024. All rights reserved.