发布表单数据失败

时间:2014-11-13 21:30:10

标签: python python-2.7 urllib2

创建了一个表单:

<form action="posting.php" method="post" name="post" onsubmit="return checkForm(this)">

当我将表单的数据发布到posting.php时,它会将我发回表单所在的页面。我尝试手动输入表单而没有填写字段,当页面中缺少某些内容时,它会在页面上显示错误消息,当我提交时,页面的HTML中没有这些消息(我将其打印到shell以查看它的位置)。

我尝试使用机械化但没有成功。

根据机械化的形式:

<TextControl(subject=)>
<TextControl(sub_title=)>
<HiddenControl(<None>=black) (readonly)>
<SelectControl(addbbcode18=[*#, darkred, red, orange, brown, yellow, green, olive, cyan, blue, darkblue, indigo, violet, white, black])>
<HiddenControl(<None>=12) (readonly)>
<SelectControl(addbbcode20=[7, 9, *12, 18, 24])>
<HiddenControl(<None>=:)) (readonly)>
<TextareaControl(message=)>
<HiddenControl(=) (readonly)>
<HiddenControl(mode=newtopic) (readonly)>
<HiddenControl(f=1521) (readonly)>
<HiddenControl(last_post=0) (readonly)>
<CheckboxControl(disable_bbcode=[on])>
<CheckboxControl(disable_smilies=[on])>
<CheckboxControl(notify=[*on])>>

我的发布代码(post_url是“domainname.posting.php”):

def post_data(url, values={}): #either read a page or post to a page
    if values != {}: #if values were provided, post request
        data = urllib.urlencode(values) #encode the valyes
        req = urllib2.Request(url, data=data) #make the request
        resp = urllib2.urlopen(req) #get the response
        return resp.read() #return the read page
    else: #no values, normal read request
        req = urllib2.Request(url) #request the url
        resp = urllib2.urlopen(req) #get the reponse 
        return resp.read() #return the read page

print post_data(post_url, {"subject":"Test Post","sub_title":"","message":"Test","<None>":"black", "addbbcode18":"black","<None>":12,"addbbcode20":"12","<None>":":","last_post":"0","disable_bbcode":"on","disable_smilies":"on","notify":"on",None:None,"mode":"newtopic","f":"1521"})

表单数据顺序是否需要匹配表单的顺序(如机械化所示)?也许我为表单提供了无效的值类型(例如“disable_bbcode”:“on”,如果它是“disable_bbcode”:“[* on]”?)。它可能是表单声明中的onsubmit,如果这是问题,我该如何提交呢?

注意:

  • 在打印出来的html中,“subject”和“message”的值为空,所以我猜测这些值永远不会发布。
  • 我怀疑“模式”和“f”是GET方法,但是当我尝试发布到“posting.posp?mode = newtopic&amp; f = 1521”时(从发布的值中删除“mode”和“f”) ,我得到了相同的结果。

0 个答案:

没有答案