在nodeJs中使用XRay从网站检索数据

时间:2018-07-26 11:19:13

标签: javascript node.js web-scraping selector x-ray

我刚刚在nodeJs中发现了XRay库。 到目前为止,我一直都使用puppeteer进行网络抓取,但是在网络上阅读时,我发现这种替代方法是进行一些测试,在我看来,它比puppeteer快一点。 因此,我决定以一种简单的方式使用它来测试功能。 可能它的功能少于木偶手,但就我的目的而言可能是个好主意。无论如何,在一次测试中,我试图提取标签的文本内容,但没有成功。我按照指南进行操作,但没有得到预期的结果。 这里是我要从中提取文本的html代码:

<div class="feature-data">
    <div class="feature-icon rooms-on" title="Totale Locali:"></div>
    <span class="gallery-attr-item-value">3</span>
    <div class="feature-icon beds-on" title="N. Camere da Letto:"></div>
    <span class="gallery-attr-item-value">2</span>
    <div class="feature-icon baths-on" title="Numero Bagni"></div>
    <span class="gallery-attr-item-value">1</span>
    <div class="feature-icon totalsqm-on" title="Mq:"></div>
    <span class="gallery-attr-item-value"> 69 </span>
    <div class="feature-icon energy-sprite" title="Classe Energetica"></div>
    <span class="gallery-attr-item-value">D</span>
</div>

我只想提取第一个span标签中的文本。 这里是我用来实现第一个目标的代码:

x(''myUrl, {
    data: x('.feature-data',{
        number: 'span'
    })(function(err, title) {
        if(err){
            console.log('error');
        }else{
           console.log('num: '+title);
         }
    })
});

实际上我收到了一个空结果。我也尝试将JSON.parse和JSON.stringify与title变量一起使用,但是结果是相同的。 我希望有一个简单的理由来解释这个问题。

0 个答案:

没有答案