I want to get some listing data from YellowPages.com using Google Apps Script. Using UrlFetchApp.fetch(url)
in GAS (as shown below), the server throws a 500 error. However, I can successfully use IMPORTXML()
inside a Google Sheet on the same URL and it works fine.
What explains this difference in behavior? And what can I do differently in Google Apps Script to achieve the same, desired result I'm getting from IMPORTXML()
?
Google Sheets 
=IMPORTXML("https://www.yellowpages.com/al/accounting-services", "//div[@class='v-card']//a/@href")
The behavior is as expected. The result is an array of links.
Google Apps Script 
Code.gs
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;
}
The behavior not as expected. The result is a 500 error.
Error message: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")
Why is the UrlFetchApp.fetch(url)
method throwing an error and what can be done to make it behave like IMPORTXML()
?