Rav*_*ven 1 html javascript node.js cheerio
我正在尝试将URL变量中页面的li标记中包含的URL归零。它应该很简单,但是我无法使它正常工作。我得到了正确数量的元素,但是它们都是空白的。text()返回''&html()返回null。我在这里做错了什么?
const cheerio = require('cheerio');
const request = require('request');
function getHistory(){
let url = 'http://coinmarketcap.com/historical/';
request(url,(error,response,html)=>{
var $ = cheerio.load(html);
$('li.text-center').each((i,element)=>{
var omg = $(this).html();
console.log(omg);
});
});
}
Run Code Online (Sandbox Code Playgroud)
与实际的jQuery不同,cheerio显然没有设置的值this。如果您更改此设置:
var omg = $(this).html();
Run Code Online (Sandbox Code Playgroud)
对此:
var omg = $(element).html();
Run Code Online (Sandbox Code Playgroud)
您将看到期望的HTML。
如果您真正想要的是href,则应<a>使用选择器定位标签,并从中获取实际href属性。您可以这样做:
function getHistory(){
let url = 'http://coinmarketcap.com/historical/';
request(url,(error,response,html)=>{
let $ = cheerio.load(html);
$('li.text-center a').each((i,element)=>{
let omg = $(element).attr('href');
console.log(omg);
});
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1584 次 |
| 最近记录: |