我正在使用 cheerio
和 nodejs
来抓取网站上所有可用的国家/地区,基本上我是这样做的:
const rp = require('request-promise');
const cheerio = require('cheerio');
const options =
{
uri: 'https://uk.soccerway.com/',
transform: function(body)
{
return cheerio.load(body);
}
};
rp(options)
.then(($) =>
{
$('#navbar-left > div:eq(2) > select > option').each(function()
{
console.log($(this).val());
});
}).catch((err) =>
{
console.log(err);
})
我想要获取 Club Domestic 的所有国家/地区,不幸的是我的代码返回以下内容:
SyntaxError: unmatched pseudo-class :eq
那是什么?
更新
新代码(迭代不会开始):
request('https://uk.soccerway.com/', function(err, resp, html)
{
if (!err)
{
const $ = cheerio.load(html);
var countriesMenu = find($, '#navbar-left > div:eq(2) option');
$(countriesMenu).each(function()
{
console.log($(this).val());
});
}
});
最佳答案
正如评论中提到的,cheerio 不支持伪类 :eq。您在这里没有使用 jQuery,$ 是 Cheerio 对象。
这是一个您可以使用的插件:https://github.com/watson/cheerio-eq
关于javascript - 无法从特定选择中获取选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49542828/