使用cheerio在br标签后的目标文本 [英] target text after br tag using cheerio

查看:39
本文介绍了使用cheerio在br标签后的目标文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cheerio抓取来创建API.我正在从这个相当复杂的站点抓取信息: http://www.vegasinsider.com/nfl/odds/las-vegas/

I'm practicing creating an API by scraping using cheerio. I'm scraping from this fairly convoluted site: http://www.vegasinsider.com/nfl/odds/las-vegas/

我正在尝试在此< td> 元素的定位标记内的这些< br> 标记之后定位文本:

I'm trying to target the text after these <br> tags within the anchor tag in this <td> element:

<td class="viCellBg1 cellTextNorm cellBorderL1 center_text nowrap" 
      width="56">
   <a class="cellTextNorm" href="/nfl/odds/las-vegas/line-movement/packers-@- 
       bears.cfm/date/9-05-19/time/2020#BT" target="_blank">
        &nbsp;<br>46u-10<br>-3½&nbsp;-10
   </a>
 </td>

以下代码是我用来定位所需数据的代码.我遇到的问题是我不知道如何在< br> 标记后获取该文本.我已经尝试过.find('br'),但无法正常工作.这是代码:

The code below is what i'm using to target the data I want. The problem I'm having is I don't know how to get that text after the <br> tags. I've tried .find('br') and couldn't get it to work. Here is the code:

app.get("/nfl", function(req, res) {
  var results = [];

  axios.get("http://www.vegasinsider.com/nfl/odds/las-vegas/").then(function(response) {
    var $ = cheerio.load(response.data);

    $('span.cellTextHot').each(function(i,element) {
      // console.log($(element).text());
      var newObj = {
        time:$(element).text()
      }
      $(element).parent().children().each(function(i,thing){
        if(i===2){
          newObj.awayTeam = $(thing).text();
        }
        else if (i===4){
          newObj.homeTeam = $(thing).text();
        }
      });
      newObj.odds= $(element).parent().next().next().text().trim();
      $('.frodds-data-tbl').find('td').next().next().children().each(function(o, oddsThing){
        if(o===0){
          newObj.oddsThing = $(oddsThing).html();
        }
      });
    res.json(results);
  });
});

您可以看到我能够将此框中的所有文本输出到newObj.odds值.我试图使用类似于下一行的内容,我将td元素作为目标并循环遍历并将其分成自己的newObj属性,例如newObj.oddsLine1和newObj.oddsLine2.

You can see I am able to output all the text in this box to the newObj.odds value. I was trying to use something like the next line where I'm targeting that td element and loop through and break out each row into its own newObj property, newObj.oddsLine1 and newObj.oddsLine2 for example.

希望如此.任何帮助是极大的赞赏.

Hope that makes sense. Any help is greatly appreciated.

推荐答案

您无法选择带有cheerio的文本节点,需要使用js dom属性/函数:

You can't select text nodes with cheerio, you need to use js dom properties / functions:

$('td a br')[0].nextSibling.nodeValue

注意$(css)[0]将给您第一个元素作为js对象(而不是cheerio对象)

Note $(css)[0] will give you the first element as a js object (rather than a cheerio object)

这篇关于使用cheerio在br标签后的目标文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆