Google apps script 在Google Apps脚本中使用UrlFetchApp获取网页并解决服务器错误代码500
我想使用谷歌应用程序脚本从YellowPages.com获取一些列表数据。使用GAS中的Google apps script 在Google Apps脚本中使用UrlFetchApp获取网页并解决服务器错误代码500,google-apps-script,web-scraping,google-sheets,internal-server-error,server-error,Google Apps Script,Web Scraping,Google Sheets,Internal Server Error,Server Error,我想使用谷歌应用程序脚本从YellowPages.com获取一些列表数据。使用GAS中的UrlFetchApp.fetch(url)(如下所示),服务器抛出500错误。但是,我可以在同一URL上的Google工作表中成功地使用IMPORTXML(),而且效果很好 是什么解释了这种行为上的差异?在Google Apps脚本中,我可以做些什么不同的事情来实现我从IMPORTXML()获得的相同的、期望的结果 谷歌表单 行为如预期。结果是一个链接数组 谷歌应用程序脚本 代码.gs 行为不符合预期。结果
UrlFetchApp.fetch(url)
(如下所示),服务器抛出500错误。但是,我可以在同一URL上的Google工作表中成功地使用IMPORTXML()
,而且效果很好
是什么解释了这种行为上的差异?在Google Apps脚本中,我可以做些什么不同的事情来实现我从IMPORTXML()
获得的相同的、期望的结果
谷歌表单
行为如预期。结果是一个链接数组
谷歌应用程序脚本
代码.gs
行为不符合预期。结果是500个错误
错误消息:
Exception: Request failed for https://www.yellowpages.com returned code 500. Truncated server response: <html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>openresty</... (use muteHttpExceptions option to examine full response) (line 16, file "Code")
异常:请求失败https://www.yellowpages.com 返回代码500。截断的服务器响应:
500内部服务器错误
500内部服务器错误
openrest我认为这个网站可能会以某种方式阻止从UrlFetchApp
发出的请求(我尝试使用其他几种工具发出请求,但在其他任何地方都能正常工作)。根据您的具体情况,您可能希望将脚本发布为web应用程序并使用。@Iamblichus:但是服务器可能会看到什么,让它将UrlFetchApp
标识为请求的源?它们都是未经验证的GET
请求,对吗?谷歌服务器的IP地址是我唯一能想到的。但是,假设谷歌循环使用其服务器,它应该只会被所有请求的一个子集阻止。对的想法?例如,如果您仍在寻找这个问题的解决方案,那么这第一个脚本有用吗?另外,关于您的问题的原因,在“添加:”部分的实验如何?我认为这个网站可能会以某种方式阻止从UrlFetchApp
发出的请求(我尝试使用其他几种工具发出请求,但在其他任何地方都能正常工作)。根据您的具体情况,您可能希望将脚本发布为web应用程序并使用。@Iamblichus:但是服务器可能会看到什么,让它将UrlFetchApp
标识为请求的源?它们都是未经验证的GET
请求,对吗?谷歌服务器的IP地址是我唯一能想到的。但是,假设谷歌循环使用其服务器,它应该只会被所有请求的一个子集阻止。对的想法?例如,如果您仍在寻找这个问题的解决方案,那么这第一个脚本有用吗?还有,关于你的问题的原因,在“补充:”部分的实验怎么样?
const ENDPOINT = 'https://www.yellowpages.com/al/accounting-services';
const main = () => {
const response = UrlFetchApp.fetch( ENDPOINT, );
const responseContentText = response.getContentText();
Logger.log('(line 5) responseContentText\n%s', responseContentText,);
return responseContentText;
}
Exception: Request failed for https://www.yellowpages.com returned code 500. Truncated server response: <html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>openresty</... (use muteHttpExceptions option to examine full response) (line 16, file "Code")