使用Python登录网站访问数据

2024-09-26 22:09:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我订阅了站点https://www.naturalgasintel.com/,以获取直接以.txt文件显示在其站点上的每日数据源;其用户登录页为https://www.naturalgasintel.com/user/login/

例如,今天的提要的文件由链接https://naturalgasintel.com/ext/resources/Data-Feed/Daily-GPI/2019/01/20190104td.txt给出,并显示在站点上,如下图所示:

enter image description here

我想做的是使用我的user_emailuser_password登录,并将这些数据以Excel文件的形式保存下来。你知道吗

当我使用Twill尝试通过首先将我登录到站点来“指向”我的数据时,我使用以下代码:

from email.mime.text import MIMEText
from subprocess import Popen, PIPE
import twill
from twill.commands import *

year= NOW[0:4]
month=NOW[5:7]
day=NOW[8:10]
date=(year+month+day)

path = "https://naturalgasintel.com/ext/resources/Data-Feed/Daily-GPI/"
end = "td.txt"

go("http://www.naturalgasintel.com/user/login")
fv("2", "user[email]", user_email)
fv("2", "user[password]", user_password)
fv("2", "commit", "Login")

datafilelocation = path + year + "/" + month + "/" + date + end
go(datafilelocation)

但是,当我转到数据的位置时,从用户登录页登录会将我发送到这个referer链接。你知道吗

https://www.naturalgasintel.com/user/login?referer=%2Fext%2Fresources%2FData-Feed%2FDaily-GPI%2F2019%2F01%2F20190104td.txt

而不是:

https://naturalgasintel.com/ext/resources/Data-Feed/Daily-GPI/2019/01/20190104td.txt

我也尝试过使用requests之类的模块从站点登录,然后访问这些数据,但是无论我使用什么方法,都会将我发送到HTML源代码,而不是.txt数据位置本身。你知道吗

我已经发布了我对python2.7模块Twill的完整介绍,我在这里附加了一个赏金:

Using Twill to grab .txt from login page Python

访问这些受密码保护的文件的最佳解决方案是什么?你知道吗


Tags: 文件数据fromhttpsimporttxtcom站点
1条回答
网友
1楼 · 发布于 2024-09-26 22:09:06

如果你有一个兼容的FireFox版本来实现这个功能,那么你可以通过Chee获得插件JavaScript0.0.1,并添加以下内容来在页面上运行:

document.getElementById('user_email').value = "E-What";
document.getElementById('user_password').value = " ABC Password ";

根据需要更改电子邮件和密码。它将加载页面,然后它会把你的用户名和密码。你知道吗

还有其他方法可以通过自己的独立流程来完成这一切。你不必下载其他人的程序,并尝试学习他们(除了这个小东西),如果你改变了这种方式。你知道吗

我本来会把这个问题投赞成票的。你知道吗

相关问题 更多 >

    热门问题