python - 使用 Twill 从登录页面 Python 中抓取 .txt

标签 python python-2.7 authentication twill

我正在使用 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() 的原因是当我到达页面时它给出我这个:

显示形式()

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"

解决这个问题的最佳方案是什么?

最佳答案

如果您愿意使用 Mechanize 而不是 Twill,请尝试以下方法:

import mechanize

username = ""
password = ""
login_post_url = "http://www.naturalgasintel.com/user/login"
internal_url = "https://naturalgasintel.com/ext/resources/Data-Feed/Daily-GPI/2018/12/20181221td.txt"

browser = mechanize.Browser()
browser.open(login_post_url)
browser.select_form(nr = 1)
browser.form['user[email]'] = username
browser.form['user[password]'] = password
browser.submit()

response = browser.open(internal_url)
print response.read()

关于python - 使用 Twill 从登录页面 Python 中抓取 .txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53890657/

相关文章:

python - 为同一进程创建新控制台

python - 将命令提示符输出重定向到 python 生成的窗口

cookies - 并发用户 session - 为什么我们看不到更多?

javascript - Facebook API : How to authenticate a web app

authentication - 如何使用 JavaMail 调试 SMTP 身份验证错误?

python - 名称错误 : name 'add' is not defined

python - 如何保存memcache值直到配额被补充?

python - pickle 比使用数字数据的 cPickle 更快?

python - 如何在 Python 中为 "in"关键字指定自定义比较器?

python - 使用带有 Initializer 的对象方法(同一行)