调用函数时不能将网站 link 作为参数包含在内
Can't include website link as a parameter while calling a function
我使用 request
和 cheerio
库创建了一个脚本,以从网站获取不同的 post 标题及其相应链接。该脚本似乎运行良好。如果您查看下面的脚本,您会发现我使用了 getposts((item,link) => console.log({item,link}));
来调用该函数。
现在,问题是:
How can I include startUrl (website link) as a parameter while calling the function keeping the rest of the logics as they are?
var request = require('request');
var cheerio = require('cheerio');
const startUrl = 'https://whosebug.com/questions/tagged/web-scraping';
function getposts(callback) {
request(startUrl, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('.summary .question-hyperlink').each(function() {
var items = $(this).text();
var links = $(this).attr('href');
return callback(items, links);
});
}
});
}
getposts((item,link) => console.log({item,link}));
为url创建一个新参数并传入startUrl:
var request = require('request');
var cheerio = require('cheerio');
const startUrl = 'https://whosebug.com/questions/tagged/web-scraping';
function getposts(url, callback) {
request(url, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('.summary .question-hyperlink').each(function() {
var items = $(this).text();
var links = $(this).attr('href');
return callback(items, links);
});
}
});
}
getposts(startUrl, (item,link) => console.log({item,link}));
我使用 request
和 cheerio
库创建了一个脚本,以从网站获取不同的 post 标题及其相应链接。该脚本似乎运行良好。如果您查看下面的脚本,您会发现我使用了 getposts((item,link) => console.log({item,link}));
来调用该函数。
现在,问题是:
How can I include startUrl (website link) as a parameter while calling the function keeping the rest of the logics as they are?
var request = require('request');
var cheerio = require('cheerio');
const startUrl = 'https://whosebug.com/questions/tagged/web-scraping';
function getposts(callback) {
request(startUrl, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('.summary .question-hyperlink').each(function() {
var items = $(this).text();
var links = $(this).attr('href');
return callback(items, links);
});
}
});
}
getposts((item,link) => console.log({item,link}));
为url创建一个新参数并传入startUrl:
var request = require('request');
var cheerio = require('cheerio');
const startUrl = 'https://whosebug.com/questions/tagged/web-scraping';
function getposts(url, callback) {
request(url, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('.summary .question-hyperlink').each(function() {
var items = $(this).text();
var links = $(this).attr('href');
return callback(items, links);
});
}
});
}
getposts(startUrl, (item,link) => console.log({item,link}));