Python mechanize login to website(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 机械化登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 机械化登录网站
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
