python - 使用 Python 提交表单

标签 python mechanize

我最近一直在处理表单提交,我正在编写一个 Python 脚本来查看是否可以仅通过输入验证码来创建 Steam 帐户。作为引用,我要提交的网站是 https://store.steampowered.com/join/ .如 Mechanize 请求所示,要填写的表格如下所示:

<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.根据我的一些测试,我认为该行可能确实有效,但 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 完成任务的替代方法,我将非常感激。我努力寻找但失败了。如果可以,请伸出援手!

最佳答案

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

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

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

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

关于python - 使用 Python 提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650499/

相关文章:

python - 在Python中,如何进行groupby+transform+设计的函数来移动一列中的值?

python - 如何在 Python 中制作一个以逗号结尾的列表?

javascript - 使用 Mechanize for Python 模拟简单的 javascript (location.href=)

python-3.x - Python + Mechanize - 使用 POST 模拟 Javascript 按钮单击?

javascript - Python Mechanize 上传

python - HTML Flask 按钮无法正确重定向到主页 URL

python - 使用默认分隔符与用户定义的分隔符拆分的字符串

ruby - 第一次获取请求后,Ruby Mechanize 的速度如何?

python - 重新排序 csv 中的列但字典损坏

python - 在 python 装饰器中引发异常是一个好的模式吗?