gpt4 book ai didi

google-apps-script - UrlFetchApp.fetch() 似乎没有改变用户代理

转载 作者:行者123 更新时间:2023-12-02 14:50:19 26 4
gpt4 key购买 nike

尝试使用 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 是用户代理,如果我没有包含 header ,我从 GAS 代码中得到的错误通常是从 Python 代码中得到的错误。我是 js 的新手,但据我所知这是正确的方法..?

编辑:现在标题在正确的位置,但问题仍然存在,与之前的错误完全相同。

var options = {"headers": {"User-Agent": "testagent"}};
var page = UrlFetchApp.fetch("someurl?as_data_structure", options);

最佳答案

星★(左上)本期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.

它也没有准确返回将要发出的请求。

关于google-apps-script - UrlFetchApp.fetch() 似乎没有改变用户代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com