python - 使用 Python 2.7 登录密码保护站点的问题
问题描述
我正在使用 Twill 检索包含所需 .txt 数据的页面,以便将它们存储为 Excel 文件。数据受密码保护,所以我从/user/login
页面登录。
我的代码遇到了一个问题,它试图从登录屏幕访问文本页面并碰到 HTML 的砖墙,而不是 .txt 本身。
当我运行登录时:
path = "https://naturalgasintel.com/ext/resources/Data-Feed/Daily-GPI/"
end = "td.txt"
go("http://www.naturalgasintel.com/user/login")
showforms()
fv("2", "user[email]", user_email)
fv("2", "user[password]", user_password)
fv("2", "commit", "Login")
datafilelocation = path + year + "/" + month + "/" + date + end
go(datafilelocation)
当我的代码到达时,go(datafilelocation)
我得到了这个:
==> at https://www.naturalgasintel.com/user/login?referer=%2Fext%2Fresources%2FData-Feed%2FDaily-GPI%2F2018%2F12%2F20181221td.txt
Out[18]: u'https://www.naturalgasintel.com/user/login?referer=%2Fext%2Fresources%2FData-Feed%2FDaily-GPI%2F2018%2F12%2F20181221td.txt'
referer
所以当我真的想进入页面时,它指向而不是实际的文本:
https://naturalgasintel.com/ext/resources/Data-Feed/Daily-GPI/2018/12/20181221td.txt
我使用fv("2", "commit", "Login")
而不是的原因submit()
是,当我到达页面时,它给了我这个:
showforms()
Form name=quick-search (#1)
## ## __Name__________________ __Type___ __ID________ __Value__________________
1 q text q Search
Form #2
## ## __Name__________________ __Type___ __ID________ __Value__________________
1 utf8 hidden (None) ✓
2 authenticity_token hidden (None) pnFnPGhMomX2Lyh7/U8iGOZKsiQnyicj7BWT ...
3 referer hidden (None) https://www.naturalgasintel.com/ext/ ...
4 popup hidden (None) false
5 user[email] text user_email
6 user[password] password user_pas ...
7 user[remember_me] hidden (None) 0
8 user[remember_me] checkbox user_rem ... None
9 commit submit (None) Login
然后它在我之后告诉我submit()
:
Note: submit is using submit button: name="commit", value="Login"
我不知道如何正确登录该站点,因此我不必使用用户登录页面,因为我认为这是导致问题的原因。
如何绕过referrer
参数从主页登录并获取正确的url?
解决方案
推荐阅读
- python-3.x - 卷积神经网络的图像预处理
- javascript - 2 种写 Plotly.newPlot 的方法:有区别吗?
- python - 循环遍历非 None 和非空目录
- database - Sonarqube - 非常大的数据库
- c# - 如何在 C# 中为未处理的异常全局应用异常(应用程序是 WPF、Winforms 和类库项目的组合)
- java - Tomcat 日志记录属性过滤
- github - 从 github 获取最新的 pkg
- java - Java 使用嵌套的 for 循环打印模式
- c++ - GCC编译器空值错误
- c# - 无法使用 Galasoft MVVM 显示 TreeView 项目