【问题标题】:Form Submission with Python使用 Python 提交表单
【发布时间】:2011-12-27 23:55:07
【问题描述】:

我最近一直在搞乱表单提交,我正在制作一个 Python 脚本,看看我是否可以通过仅输入验证码来创建 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()

如果有人对可能出现的问题有任何任何想法,甚至有使用 Python 完成任务的替代方法,我将不胜感激。我已经努力寻找并且失败了。如果可以,请伸出援手!

【问题讨论】:

    标签: python mechanize


    【解决方案1】:

    获取页面https://store.steampowered.com/join/并搜索正则表达式&lt;input type="hidden" id="captchagid" name="captchagid" value="(.+)"&gt;

    验证码 URL 为 https://store.steampowered.com/public/captcha.php?gid=&lt;gid&gt;

    使用 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
    

    【讨论】:

    • 真的每次都和 &count=4 一起工作吗?以及如何提交这样的 POST 请求?
    • @Paul urllib.urlopen(url, post_data)
    • 我通过一些搜索发现了这一点。显然,如果您不重复使用验证码,计数并不重要。一切都很好!谢谢你。
    猜你喜欢
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2011-10-31
    • 1970-01-01
    相关资源
    最近更新 更多