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

查看:87
本文介绍了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数据的确以应有的方式进行了结构化.但是,似乎机械化弄乱了我的urlencoding(我已经对网站专用的字符集的值进行了urlencode).例如,我的用户名包含Å-我已将其urlencode到%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.

推荐答案

机械化似乎仍然会对字符串进行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天全站免登陆