var result = [];
for(var i =1; i <= 20; i++){
var url = 'https://example.com/page/'
var resp = UrlFetchApp.fetch(url, options).getContentText();
var $ = Cheerio.load(resp);
var jobList = $(".views-field.views-field-title > a");
for(let i = 0; i < jobList.length; i++) {
// console.log(jobList[i].getAttribute("href"))
var jobUrl = 'https://example.com' + /href="(.+?)">/.exec(jobList[j])[1];
var data = scrapeJobDetails(jobUrl);
if(data != null){
result.push(...data);
}
}
}
var sheet = ss.getSheetByName('Sheet1');
sheet.getRange('A2:Z').clearContent();
sheet.getRange(sheet.getDataRange().getLastRow() + 1, 1, result.length, result[0].length).setValues(result);
}
i dont know where am getting it wrong.i want to extract all urls in a class. below is class tag from the web page
<td class="views-field views-field-title">
<a href="xxxxxxx" class="cat-job-rate recruiter-colorbox-processed">
xxxxxx
</a><br>
<span class="job-label">Organization:</span>
xxxxx | <span class="job-label">xxxxxx:</span>
xxxx | <span class="job-label">xxxx:</span> xxxxx
</td>
There are several things wrong with your code. I removed some for loops that were not needed and restructured your code to what I think you're trying to do. I'm assuming your response is loading correctly too. This code will collect your urls in an array then go through each url in your array to run your scrapeJobDetails function.
Make sure you are collecting the correct information before adding your google sheets code.
var result = [];
var url = 'https://example.com/page/';
var resp = UrlFetchApp.fetch(url, options).getContentText();
var $ = Cheerio.load(resp);
var jobList = $(".views-field.views-field-title > a");
var urls = jobList.map(function() {return $(this).attr('href');}).toArray();
// debug code - outputs the urls it collected
console.log(urls);
for (let i = 0; i < urls.length; i++) {
var data = scrapeJobDetails(urls[i]);
if (data != null) {
result.push(...data);
}
}