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 机械化登录网站
基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
