Http Post请求返回HTML而不是Csv数据 [英] Http Post request returns HTML instead of Csv data

查看:44
本文介绍了Http Post请求返回HTML而不是Csv数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从ASPX网站上获取 csv .没有直接链接到 csv ,它是发布表单时由服务器生成的.

I am trying to fetch a csv off of an aspx website. There is no direct link to the csv, it is server generated when posting the form.

以下内容可以在使用UrlFetchApp的Google App脚本中正常运行:它返回原始的 .csv 数据.

The following works correctly in Google App Script using UrlFetchApp: It returns raw .csv data.

  ..// other code omitted for brevity

  var page = UrlFetchApp.fetch(
    "https://www.mycompanyurl.com/summary.aspx", {
    "method": "POST",
    "headers": {"Cookie": myCookieData},
    "payload": {
      '_ctl0:contact:isActive': '1',
      '__VIEWSTATE': myViewstateData,
      '__EVENTTARGET': '_ctl0:Main:ButtonExport',
      '__EVENTARGUMENT': '',
      '__LASTFOCUS': '',
      '__VIEWSTATEENCRYPTED': '',
      '__PREVIOUSPAGE': ''
    }
});

Logger.log(page); // <---- prints the csv file content

我试图在python请求库中模拟这一点.

I've tried to emulate this in python requests library.

..// other code omitted for brevity
..// cookies are in the session 's'

page = s.post(
  'https://www.mycompanyurl.com/summary.aspx', 
  data={
    '_ctl0:contact:isActive': '1',
    '__VIEWSTATE': myViewstateData,
    '__EVENTTARGET': '_ctl0:Main:ButtonExport',
    '__EVENTARGUMENT': '',
    '__LASTFOCUS': '',
    '__VIEWSTATEENCRYPTED': '',
    '__PREVIOUSPAGE': ''
  }
);

print(page) // <---- prints the html content of the page

我省略了我尝试过的其他2个以相同结果结尾的库.axios(node.js)和请求(node.js)

I have omitted 2 other libraries I tried that ended in the same result. axios (node.js) and request (node.js)

为什么请求不能在Google Script上运行,而在其他更受欢迎的库中却不行.

Why does the request work on Google Script but not in other, more popular libraries.

我可能缺少一些简单的东西,但是在尝试了不同的标头配置和多个库很多小时之后,我很茫然.

Probably something simple I am missing, but after many hours of trying different header configurations and multiple libraries, I am at a loss.

推荐答案

两个代码段之间的第一个区别是您没有将cookie传递给Python版本.

The first difference I can see between the two code snippets is that you are not passing your cookies into the Python version.

如果您查看作为响应的HTML,那将是有益的.也许有一些错误消息(例如:请登录).

It would be beneficial if you had a look at the HTML you got in response. Perhaps there's some error message (like: Please log in).

尝试将cookie添加到请求中,看看是否有帮助.

Try adding your cookies to the request and see if that helps.

这篇关于Http Post请求返回HTML而不是Csv数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆