我正在尝试在我的网页上运行一个简单的抓取,但我遇到的问题是我似乎无法正确选择带有我的抓取器的 HTML 元素。我以为我从我的 中正确获取了属性<li>
,但是我得到了undefined
每个对象属性的值。
我正在寻找data-name
,data-address
和data-url
值。
scrape.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('name');
var address = li.attr('address');
var url = li.attr('url');
var metadata = {
name : name,
address : address,
url : url
};
console.log(metadata);
});
}
});
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="Accel Partners" 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>