Python 机械化登录网站 [英] Python mechanize login to website

查看:40
本文介绍了Python 机械化登录网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 和 Mechanize 登录网站,但是,在尝试让 POST 数据按我想要的方式运行时遇到了麻烦.

I'm trying to log into a website using Python and Mechanize, however, I'm running into trouble when trying to get the POST data to behave as I want.

基本上我想使用机械化和 Python 来复制这个:

Essentially I want to replicate this using mechanize and Python:

wget --quiet --save-cookies cookiejar --keep-session-cookies --post-data "action=login&login_nick=USERNAME&login_pwd=PASSWORD" -O outfile.htm http://domain.com/index.php

表格如下所示:

<login POST http://domain.com/index.php application/x-www-form-urlencoded
  <TextControl(login_nick=USERNAME)>
  <PasswordControl(login_pwd=PASSWORD)>
  <CheckboxControl(login_auto=[1])>
  <SubmitButtonControl(<None>=) (readonly)>>

设置适当的值并提交表单不是问题,但这会忽略action=login"部分.

Setting the appropriate values and submitting the form isn't a problem, but that leaves out the "action=login"-part.

response = self.browser.open(self.url+"/index.php")
self.browser.select_form(name="login")

self.browser["login_nick"] = self.encoded_username
self.browser["login_pwd"] = self.encoded_password

self.browser.method = "POST"

response = self.browser.open(self.browser.submit())

print (response.read())

现在的问题是,如何添加 action=login 部分?

Now the question is, how do I add the action=login part?

好的,所以我添加了一个名为 action 的隐藏字段,并将值设置为 login.使用 Wireshark 分析 TCP 流,POST 数据确实按其应有的方式构建.但是,似乎 mechanize 弄乱了我的 urlencoding(我已经专门为网站使用的字符集对值进行了 urlencoded).例如,我的用户名包含一个 Å - 我已将其编码为 %C5.但是,当它与机械化一起发送时,它显示为 %25C5.如何阻止机械化改变琴弦?

Okay, so I added a hidden field named action and set the value to login. Analyzing the TCP stream with Wireshark, the POST data is indeed structured the way it should. However, it seems that mechanize is messing with my urlencoding (I have already urlencoded the values specifically for the charset that the website uses). For example, my username contains an Å - which I have urlencoded to %C5. However, when it's sent with mechanize, it's displayed as %25C5. How do I stop mechanize from changing the strings?

我意识到我可以在发送字符串之前对我的字符串进行urlencode,而不是与机械化作斗争.案件结案.

I realized that rather than fighting mechanize, I could just not urlencode my strings before sending them. Case closed.

推荐答案

Mechanize 似乎无论如何都会对字符串进行 urlencode,所以没有必要与它作斗争.这是最终的解决方案(显然在语法上无效,但希望您能理解).

Mechanize seems to urlencode the strings anyway, so there's no point in fighting it. This is the final solution (obviously not syntactically valid, but hopefully you get the idea).

import mechanize

self.browser = mechanize.Browser()
self.browser.open(self.url)
self.browser.select_form(name="login")

self.browser["login_nick"] = self.username
self.browser["login_pwd"] = self.password
self.browser.new_control("HIDDEN", "action", {})
control = self.browser.form.find_control("action")
control.readonly = False
self.browser["action"] = "login"
self.browser.method = "POST"
self.browser.action = self.url

response = self.browser.submit()

这篇关于Python 机械化登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆