使用Python提交表单

时间:2011-12-27 23:55:07

标签: python mechanize

我最近一直在搞乱表单提交,我正在编写一个Python脚本,看看我是否可以通过输入Captcha来创建Steam帐户。作为参考,我提交的网站是https://store.steampowered.com/join/。如机械化请求所示,要填写的表格如下所示:

<create_account POST https://store.steampowered.com/join/ application/x-www-form-urlencoded
<TextControl(accountname=)>
<SelectControl(choose_accountname=[*, , ])>
<PasswordControl(password=)>
<PasswordControl(reenter_password=)>
<TextControl(email=)>
<TextControl(reenter_email=)>
<HiddenControl(challenge_question=) (readonly)>
<TextControl(secret_answer=)>
<HiddenControl(captchagid=1009037421128850761) (readonly)>
<TextControl(captcha_text=)>
<HiddenControl(action=submit_agreement) (readonly)>
<CheckboxControl(i_agree_check=[on])>
<HiddenControl(ticket=) (readonly)>>

几乎所有内容似乎都有效,但我在使用mechanize和urllib2正确提交表单方面遇到了一些麻烦。我确信我只是在做一些小而简单的错误,但我花了很长时间试图找到这个错误。我目前的要求是通过以下几个简单的方式制定的:

def submit_form(self, captcha_text):
    self.form["accountname"]=account_prefix+get_next_number()
    self.form["password"]=account_password
    self.form["reenter_password"]=account_password
    email = emails.pop()
    self.form["email"] = email
    self.form["reenter_email"] = email
    control = self.form.find_control("challenge_question")
    control.disabled = False
    control.readonly = False
    control.value = "NameOfSchool"
    self.form["secret_answer"] = secret_answer
    self.form["captcha_text"] = captcha_text
    self.form.find_control(id="i_agree_check").items[0].selected = True
    print urllib2.urlopen(self.form.click()).read()
    inc_account_number()
    resave_email_list(emails)

这个请求的大部分内容可能都是正确的,只有几行我真的认为是可疑的。使用mechanize我正在尝试使用行self.form.find_control(id="i_agree_check").items[0].selected = True检查“我同意并且是13岁或以上”框。根据我的一些测试,我认为该行可能确实有效,但ReadOnly challenge_question部分的整个设置很可能被窃听。作为参考,该代码段为:

    control = self.form.find_control("challenge_question")
    control.disabled = False
    control.readonly = False
    control.value = "NameOfSchool"

提交失败的最后可能性是提交方法:urllib2.urlopen(self.form.click()).read()

如果有人有关于可能出现问题的 ANY 想法,甚至是使用Python完成任务的替代方法,我将非常感激。我努力寻找并失败了。如果可以的话,伸出援助之手!

1 个答案:

答案 0 :(得分:0)

抓取页面https://store.steampowered.com/join/并搜索正则表达式<input type="hidden" id="captchagid" name="captchagid" value="(.+)">

验证码网址为https://store.steampowered.com/public/captcha.php?gid=<gid>

使用POST数据获取https://store.steampowered.com/join/createaccount/

    accountname=<name>&password=<password>&email=<email>&challenge_question=<question>&secret_answer=<secretansewer>&captchagid=<gid>captcha_text=<captch_text>&i_agree=1&ticket=&count=4