如何使用jquery选择某些文本节点?

时间:2022-10-31 17:47:19

I'm trying to get some data from a website using ajax.

我正在尝试使用ajax从网站获取一些数据。

This is my code with an ajax POST methode:

这是我的代码与ajax POST方法:

$(data).find('.col-md-9 .panel:lt(5)').each(function () { console.log($(this).find('.panel-body .sp-card td:eq(1) center ').text());

$(data).find('。col-md-9 .panel:lt(5)')。each(function(){console.log($(this).find('。panel-body .sp-card) td:eq(1)center')。text());

console:

安慰:

Mc-Wars Network IP: mc-wars.org Vote - Server info Website

Mc-Wars Network IP:mc-wars.org投票 - 服务器信息网站

I just want to get the first line : Mc-Wars Network

我只想获得第一线:Mc-Wars Network

Please help!

请帮忙!

Here is an image of the html code Im scraping.

这是我正在抓取的HTML代码的图像。

如何使用jquery选择某些文本节点?

Here is an image of the console after scraping. (I just need the first line.

这是刮擦后控制台的图像。 (我只需要第一行。

如何使用jquery选择某些文本节点?

This is code from the website Im scraping:

这是来自网站的代码我正在抓:

<div onclick="location.href='//www.serverpact.com/vote-20129 ';" class="panel panel-default sp-plane">
                        <div class="ribbon ribbon-small ribbon-blue">
                            <div class="banner">
                                <div style="font-size: 14px;text-align: center;" class="text">#1</div>
                            </div>
                        </div>
                        <div style="padding: 5px;" class="panel-body">
                            <table class="sp-card">
                                <tbody><tr>
                                    <td style="font-weight: bold;vertical-align: middle;"><center><i class="fa fa-thumbs-up"></i><br>8384<br><br>#1</center></td>
                                    <td style="font-weight: bold;vertical-align: middle;">
                                        <center>
                                        Mc-Wars Network<br>
                                        <b>IP: mc-wars.org<br>
                                        <a style="margin-top: 5px;" class="btn btn-primary btn-sm" href="//www.serverpact.com/vote-20129 ">Vote - Server info</a> 
                                        <a style="margin-top: 5px;" class="btn btn-success btn-sm" href="http://mc-wars.org">Website</a>
                                        </b></center>
                                    </td>
                                    <td width="500px" class="hidden-xs">
                                    <a href="http://mc-wars.org" target="_blank"><img style="margin-top: 7px;margin-bottom: 3px;border-radius: 4px;" src="http://www.serverpact.com/b/20129.gif" alt="Mc-Wars Network" class="img-responsive hidden-xs"></a>
                                        <span style="font-size: 13px;">
                                            McWars is a minecraft pvp based server where you will have to build a fortress for your team and destroy the other teams base to win. With more than 10 different classes to choose, youll love it.
                                        </span>            
                                    </td>
                                </tr>
                            </tbody></table>
                        </div>
                    </div>

4 个解决方案

#1


1  

Probably text() combines all child text nodes.

可能text()组合了所有子文本节点。

What you need is probably to retrieve the text nodes as nodes then get their content. See this post for an example.

您需要的是将节点检索文本节点然后获取其内容。有关示例,请参阅此文章。

#2


1  

Here's a snippet where I navigate the child nodes of the center in the td to the proper text node. Note that whitespace creates a child node, so for example before the center node theres a whitespace only text node (that doesn't matter in this case but it's something you should be aware of).

这是一个片段,我将td中心的子节点导航到正确的文本节点。请注意,空格会创建一个子节点,因此例如在中心节点之前只有一个空白文本节点(在这种情况下无关紧要,但这是你应该注意的事情)。

document.write('here you go:' + $('#firstDiv td:nth-child(2) center')[0].childNodes[0].textContent)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="firstDiv">
        <table id="coolTable">
           <tbody>
           <tr>
              <td>
              </td>
              <td>
                    <center>
                       The text I need!
                    <br>
                    <b>
                    The text I dont need!
                    </b>
                    </center>
             </td>
            </tr>
            </tbody>
         </table>
    </div>

See also: MDN: Whitespace in the DOM

另请参见:MDN:DOM中的空白

#3


1  

There is a way to do that:

有一种方法可以做到这一点:

$(data).find('#firstDiv #coolTable').each(function () {
console.log($.trim($(this).find('tbody tr td:eq(1) center').contents()[0].nodeValue))});

#4


1  

Ok, your question have changed a lot!
Add at the end of your .text() which will do the job:

好的,你的问题已经改变了很多!在.text()的末尾添加将完成工作:

.match(/[^\n]+/g)[0]

Note that all lines can be caught by changing the index, where "0" is the first line, "1" the second and successively.

请注意,可以通过更改索引来捕获所有行,其中“0”是第一行,“1”是第二行并且是连续的。

#1


1  

Probably text() combines all child text nodes.

可能text()组合了所有子文本节点。

What you need is probably to retrieve the text nodes as nodes then get their content. See this post for an example.

您需要的是将节点检索文本节点然后获取其内容。有关示例,请参阅此文章。

#2


1  

Here's a snippet where I navigate the child nodes of the center in the td to the proper text node. Note that whitespace creates a child node, so for example before the center node theres a whitespace only text node (that doesn't matter in this case but it's something you should be aware of).

这是一个片段,我将td中心的子节点导航到正确的文本节点。请注意,空格会创建一个子节点,因此例如在中心节点之前只有一个空白文本节点(在这种情况下无关紧要,但这是你应该注意的事情)。

document.write('here you go:' + $('#firstDiv td:nth-child(2) center')[0].childNodes[0].textContent)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="firstDiv">
        <table id="coolTable">
           <tbody>
           <tr>
              <td>
              </td>
              <td>
                    <center>
                       The text I need!
                    <br>
                    <b>
                    The text I dont need!
                    </b>
                    </center>
             </td>
            </tr>
            </tbody>
         </table>
    </div>

See also: MDN: Whitespace in the DOM

另请参见:MDN:DOM中的空白

#3


1  

There is a way to do that:

有一种方法可以做到这一点:

$(data).find('#firstDiv #coolTable').each(function () {
console.log($.trim($(this).find('tbody tr td:eq(1) center').contents()[0].nodeValue))});

#4


1  

Ok, your question have changed a lot!
Add at the end of your .text() which will do the job:

好的,你的问题已经改变了很多!在.text()的末尾添加将完成工作:

.match(/[^\n]+/g)[0]

Note that all lines can be caught by changing the index, where "0" is the first line, "1" the second and successively.

请注意,可以通过更改索引来捕获所有行,其中“0”是第一行,“1”是第二行并且是连续的。