我正在尝试运行一个Nodejs scraper,它从我的<li>
标签中获取数据属性。不幸的是,我一直接受undefined
作为我的价值观。我觉得我没有正确地使用我的代码获取数据属性,但我不确定如何准确测试。
返回:
{ name: undefined, address: undefined, url: undefined }
HTML:
<li id="v1065" data-name="Jerry's Food" data-address="5120 West 43rd Street" data-url="http://www.testsite1.com/" data-category="1"><a href="#" class="list-digital">
<span class="venue-name">Jerry's Food</span><br />
<span class="venue-address">5120 West 43rd Street</span>
</a>
</li>
<li id="v249" data-name="Mikes Pizza" data-address="13482 Seventh Avenue" data-url="http://www.testsite2.com" data-category="3"><a href="#" class="list-digital">
<span class="venue-name">Mike's Pizza</span><br />
<span class="venue-address">13482 Seventh Avenue</span>
</a>
</li>
JS:
var request = require('request');
var cheerio = require('cheerio');
request('http://personalsite.com', function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('li').each(function(i, element){
var li = $(this).contents();
var name = li.attr('data-name');
var address = li.attr('data-address');
var url = li.attr('data-url');
var metadata = {
name : name,
address : address,
url : url
};
console.log(metadata);
});
}
});
答案 0 :(得分:0)
将var li = $(this).contents();
更改为var li = $(this);
.contents()
不起作用,因为它会返回匹配元素的子元素(例如,在这种情况下为a
标记)。