STU*_*TON 2 user-agent http-headers urlfetch http-status-code-403 google-apps-script
尝试使用 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)
在此处为\xe2\x98\x85(左上角)标记问题,以便 Google 开发人员确定问题的优先级。
\n谷歌并不总是记录它的限制(烦人?)。其中一项限制是更改用户代理。它固定为
\n"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script)"\nRun Code Online (Sandbox Code Playgroud)\n你无法改变它。
\nfunction testUrlFetchAppHeaders() {\n var options = {\n headers: {\n \'User-Agent\':\n \'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36\',\n },\n };\n var fakeRequest = UrlFetchApp.getRequest(\n \'https://www.httpbin.org/headers\',\n options\n );//providing fake assurance\n var realRequest = UrlFetchApp.fetch(\n \'https://www.httpbin.org/headers\',\n options\n );//like a wrecking ball\n Logger.log({ fake: fakeRequest, real: realRequest });\n}\nRun Code Online (Sandbox Code Playgroud)\n{\n "fake": {\n "headers": {\n "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"\n },\n "method": "get",\n "payload": "",\n "followRedirects": true,\n "validateHttpsCertificates": true,\n "useIntranet": false,\n "contentType": null,\n "url": "https://www.httpbin.org/headers"\n },\n "real": {\n "headers": {\n "Accept-Encoding": "gzip,deflate,br",\n "Host": "www.httpbin.org",\n "User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script)"\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n获取请求(网址)
\n
\n\n\n\n返回调用操作时将发出的请求。
\n
\n\n\n\n该方法实际上并不发出请求。
\n
它也没有准确地返回将提出的请求。
\n| 归档时间: |
|
| 查看次数: |
3675 次 |
| 最近记录: |