简体   繁体   English

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

[英]How to select certain text nodes using jquery?

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网络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代码即时抓取的图片。

HTML代码

Here is an image of the console after scraping. 这是抓取后的控制台图像。 (I just need the first line. (我只需要第一行。

安慰

This is code from the website Im scraping: 这是来自网站Im抓取的代码:

<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>

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. 有关示例, 请参见本文

Here's a snippet where I navigate the child nodes of the center in the td to the proper text node. 这是一个片段,其中我将td center的子节点导航到正确的文本节点。 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). 请注意,空格会创建一个子节点,因此例如在center节点之前有一个仅空格的文本节点(在这种情况下这无关紧要,但是您应该意识到这一点)。

 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中的空白

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))});

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”是第二行,并依次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM