UrlFetchApp.fetch() 似乎没有改变用户代理
UrlFetchApp.fetch() doesn't seem to change user agent
尝试使用 Google Apps 脚本从网站获取数据,然后将其直接放入电子表格中。提取似乎不起作用,其中 Python 请求等效工作正常。
Python代码:
page = requests.get("someurl?as_data_structure", headers={'user-agent':'testagent'})
气体代码:
var page = UrlFetchApp.fetch("someurl?as_data_structure", headers={'user-agent':'testagent'});
唯一需要的 header 是 user-agent,我从 GAS 代码中得到的错误是我通常从 Python 代码中得到的错误,如果我没有包括 header。我是 js 的新手,但据我所知这是正确的方法..?
编辑:
现在 headers 在正确的位置,但问题仍然存在,与之前的错误完全相同。
var options = {"headers": {"User-Agent": "testagent"}};
var page = UrlFetchApp.fetch("someurl?as_data_structure", options);
headers
属于选项:
var options = {"headers": {"User-Agent": "testagent"}};
var page = UrlFetchApp.fetch("someurl?as_data_structure", options);
Star ★(左上)问题 here 供 Google 开发人员优先处理。
Google 并不总是记录它的限制(烦人?)。一个这样的限制是改变用户代理。固定为
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script)"
你不能改变它。
样本测试:
function testUrlFetchAppHeaders() {
var options = {
headers: {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
},
};
var fakeRequest = UrlFetchApp.getRequest(
'https://www.httpbin.org/headers',
options
);//providing fake assurance
var realRequest = UrlFetchApp.fetch(
'https://www.httpbin.org/headers',
options
);//like a wrecking ball
Logger.log({ fake: fakeRequest, real: realRequest });
}
响应示例:
{
"fake": {
"headers": {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
},
"method": "get",
"payload": "",
"followRedirects": true,
"validateHttpsCertificates": true,
"useIntranet": false,
"contentType": null,
"url": "https://www.httpbin.org/headers"
},
"real": {
"headers": {
"Accept-Encoding": "gzip,deflate,br",
"Host": "www.httpbin.org",
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script)"
}
}
}
getRequest(url)
Returns the request that would be made if the operation was invoked.
This method does not actually issue the request.
也没有准确return 将要提出的要求。
尝试使用 Google Apps 脚本从网站获取数据,然后将其直接放入电子表格中。提取似乎不起作用,其中 Python 请求等效工作正常。
Python代码:
page = requests.get("someurl?as_data_structure", headers={'user-agent':'testagent'})
气体代码:
var page = UrlFetchApp.fetch("someurl?as_data_structure", headers={'user-agent':'testagent'});
唯一需要的 header 是 user-agent,我从 GAS 代码中得到的错误是我通常从 Python 代码中得到的错误,如果我没有包括 header。我是 js 的新手,但据我所知这是正确的方法..?
编辑: 现在 headers 在正确的位置,但问题仍然存在,与之前的错误完全相同。
var options = {"headers": {"User-Agent": "testagent"}};
var page = UrlFetchApp.fetch("someurl?as_data_structure", options);
headers
属于选项:
var options = {"headers": {"User-Agent": "testagent"}};
var page = UrlFetchApp.fetch("someurl?as_data_structure", options);
Star ★(左上)问题 here 供 Google 开发人员优先处理。
Google 并不总是记录它的限制(烦人?)。一个这样的限制是改变用户代理。固定为
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script)"
你不能改变它。
样本测试:
function testUrlFetchAppHeaders() {
var options = {
headers: {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
},
};
var fakeRequest = UrlFetchApp.getRequest(
'https://www.httpbin.org/headers',
options
);//providing fake assurance
var realRequest = UrlFetchApp.fetch(
'https://www.httpbin.org/headers',
options
);//like a wrecking ball
Logger.log({ fake: fakeRequest, real: realRequest });
}
响应示例:
{
"fake": {
"headers": {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
},
"method": "get",
"payload": "",
"followRedirects": true,
"validateHttpsCertificates": true,
"useIntranet": false,
"contentType": null,
"url": "https://www.httpbin.org/headers"
},
"real": {
"headers": {
"Accept-Encoding": "gzip,deflate,br",
"Host": "www.httpbin.org",
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script)"
}
}
}
getRequest(url)
Returns the request that would be made if the operation was invoked.
This method does not actually issue the request.
也没有准确return 将要提出的要求。