尝试使用 Google Apps 脚本从网站获取数据,并将其直接放入电子表格中。fetch 似乎不起作用,而 Python requests 的等效项工作得很好。
Python代码:
page = requests.get("someurl?as_data_structure", headers={'user-agent':'testagent'})
Run Code Online (Sandbox Code Playgroud)
气体代码:
var page = UrlFetchApp.fetch("someurl?as_data_structure", headers={'user-agent':'testagent'});
Run Code Online (Sandbox Code Playgroud)
唯一需要的标头是用户代理,如果我没有包含标头,我从 GAS 代码中得到的错误就是我通常从 Python 代码中得到的错误。我是 js 新手,但据我所知这是正确的方法..?
编辑:现在标题位于正确的位置,但问题仍然存在,与以前的错误完全相同。
var options = {"headers": {"User-Agent": "testagent"}};
var page = UrlFetchApp.fetch("someurl?as_data_structure", options);
Run Code Online (Sandbox Code Playgroud) user-agent http-headers urlfetch http-status-code-403 google-apps-script