我正在尝试通过首先使用用户名和密码登录,然后提交后续的POST请求来自动下载报告。
我不想使用GET请求,因为这会在URL中显示我的凭据,并且历史记录会保存在浏览器中。此外,URL的结尾不是'.csv',因此我无法直接在pd.read_csv('www.something.csv')中传递该URL。
我花了很多时间来查看链接,以下内容似乎是我所需要的,但是在web_return.content中找不到我的csv。
HTTPS POST request Python, returning .csv
report_id = '45'
payload = {'un': 'user.com',
'pw': 'password',
'export': 'Export',
'enc': 'UTF-8',
'xf': 'localecsv',
'colDt_s': '1/1/2018',
'colDt_e': '1/1/2019'}
web_return = requests.post('https://website.com/{}'.format(report_id),
data=payload).content
print(web_return)
df = pd.read_csv(io.StringIO(web_return.decode('utf-8')))
我希望最后的df是作为CSV格式的熊猫数据框。我用另一种方式来工作,但是我觉得这是我应该能够做的,我想以正确的方式来做。当前输出确实是一个数据帧,但内容不正确:
print(df)
0 <html>
1 <head>
2 <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
3 <script>
4 if (this.SfdcApp && this.SfdcApp.projectOneNav...
5 if (window.location.replace){
6 window.location.replace('https://login.website...
7 } else {;
8 window.location.href ='https://login.website...
9 }
10 </script>
11 </head>
12 </html>
13 <!-- Body events -->
14 <script type="text/javascript">function bodyOn...
15 </body>
16 </html>
17 <!--
18 .................................................
19 .................................................
20 .................................................
21 .................................................
22 -->
有帮助吗?
谢谢。
-更新,感谢@Barmar,我已经能够将请求提交给Javascript,但是它告诉我我没有启用Javascript:
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</head>
<body>
<script>var tm=null;function lhdoredir(){tm&& .
(window.clearTimeout(tm),tm=null);window.location.replace? window.location.replace("https://login.website.com/45"):window.location.href="https://login.website.com/45"}window.setTimeout?tm=window.setTimeout(lhdoredir,1E3):lhdoredir()
You do not have Javascript enabled. Javascript is required to use
website.com. Please enable Javascript, then click <a
href="https://login.website.com/45">here</a> to continue.
</noscript>
<!-- service is in read write mode -->
</body>
</html>