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>値を取得します。ページからそれらを辞書に入れます。次に、ログイン時に行ったのと同様の方法で POST を試みますが、何も起こりません。

#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.

頭に浮かぶ別のアイデアは、すべての必須フィールドを POST していないということです。チェックボックスなどは送信していませんが、非表示のフィールドを含むすべてのフィールドを送信しています。これはフォーム フィールドの HTML です: http://pastebin.com/U12BXZWF

リクエストを使用してフォーラムに投稿するには、どうすればよいですか? これにより何かが変わる場合は、添付ファイルとして画像を投稿する必要もあります。

4

0 に答える 0