无法使用jquery从函数中获取值

时间:2016-01-30 19:21:04

标签: javascript jquery cheerio

我试图从<div>中获取此函数的值。我希望得到的值是“UnidosdoJaçanã”,在这个html里面:

<div class="event-data">
                <h1 class="event__title ng-binding">
                    Unidos do Jaçanã
                    <!--<a href="{{event.singleUrl}}" target="_blank"><i class="fa fa-external-link"></i></a>-->
                    <span class="event__subtitle ng-binding">Carnaval de Rua 2016</span>
                </h1>


                <!-- ngRepeat: occs in event.occurrences --><!-- ngIf: occs.inPeriod --><div class="event__occurrences ng-scope" ng-repeat="occs in event.occurrences" ng-if="occs.inPeriod">
                    <div class="event__venue">
                        <a href="http://spcultura.prefeitura.sp.gov.br/espaco/2141/" class="ng-binding">Rua Antônio César Neto, 347</a>
                    </div>
                    <div class="event__time ng-binding">Dia 6 de fevereiro de 2016 às 14:00</div><br>
                     <div class="event__time ng-binding">bloco de rua do tradicional bairro do jacana</div>
                    <!--a href="#" class="js-more-occurrences"><i class="fa fa-plus-circle"></i></a-->
                </div><!-- end ngIf: occs.inPeriod --><!-- end ngRepeat: occs in event.occurrences -->

               <!-- <div class="event__languages" style="margin: -10px 0 10px 0">
                    <h4 class="event__languages--title">{{event.terms.linguagem.length == 1 ? 'Linguagem' : 'Linguagens'}}:</h4> {{event.terms.linguagem.join(', ')}}
                </div> -->
                <span class="event__classification ng-binding">Livre</span>

                <div class="event__price ng-binding">
                    <span class="fa-stack">
                        <i class="fa fa-circle fa-stack-2x"></i>
                        <i class="fa fa-usd fa-stack-1x fa-inverse"></i>
                    </span>
                    gratuito
                </div>

                <!-- ngIf: event.traducaoLibras == 'Sim' && event.descricaoSonora == 'Sim' -->
                <!-- ngIf: event.traducaoLibras == 'Sim' && event.descricaoSonora != 'Sim' -->
                <!-- ngIf: event.traducaoLibras != 'Sim' && event.descricaoSonora == 'Sim' -->

               <!--                <div ng-if="event.project.name">
                    <h4>projeto:</h4>
                    <a href="{{event.project.singleUrl}}">{{event.project.name}}</a>
                </div>
 <div ng-if="event.owner.name">
                    <h4>publicado por:</h4>
                    <a href="{{event.owner.singleUrl}}">{{event.owner.name}}</a>
                </div> -->
                <a href="http://spcultura.prefeitura.sp.gov.br/evento/23693/" target="_blank" class="event__info">Mais informações</a>
            </div>

并且,为此,我使用jquery cheerios模块,使用此代码:

var cheerio = require('cheerio');
var request = require('request');


request({
    method: 'GET',
    url: 'http://carnavalderua.prefeitura.sp.gov.br/eventos/blocos/'
}, function(err, response, html) {
        if(err) return console.error(err)
        $ = cheerio.load(html)
        $('h1.event__title').filter(function() {
            var data = $(this)
            var title = data.html()
            console.log(title)
        })
})

我收到这个日志:

{{event.name}}
                    <!--<a href="{{event.singleUrl}}" target="_blank"><i class="fa fa-external-link"></i></a>-->
                    <span class="event__subtitle">{{event.subTitle}}</span>

我认为我正在寻找的价值是{{event.name}}。我怎么可能获得这个价值?

提前致谢。

1 个答案:

答案 0 :(得分:0)

你可以尝试正则表达式。
var fullTitle = document.querySelector('h1.event__title')
选择器将为您提供匹配

的文档中的第一个标题

然后
var titleStriped = fullTitle.innerHTML.match(/^\s*(.*)\s*</)[1]
会匹配你得到的当前HTML,并给你“UnidosdoJaçanã”

如果你对jQuery更熟悉,你也可以使用jQuery选择器,.html()方法和相同的正则表达式

注意:如果您需要匹配不同的结果,可能需要修改正则表达式