CheerioJS, looping through <ul> with same class name

Sobiaholic

I'm trying to loop through each <ul> and get the value of each <li>. The thing is, it only takes the first <ul> and skips the rest.

HTML

<div id="browse-results">
    <ul class="tips cf">
        <li>tip11</li>
        <li>tip12</li>
        <li>tip13</li>
    </ul>
    <ul class="tips cf">
        <li>tip21</li>        
        <li>tip22</li>        
        <li>tip23</li>        
    </ul>
    <ul class="tips cf">
        <li>tip31</li>        
        <li>tip32</li>        
        <li>tip33</li>        
    </ul>
    <ul class="tips cf">
        <li>tip41</li>        
        <li>tip42</li>        
        <li>tip43</li>        
    </ul>
</div>

Cheerio Parsing

$('#browse-results').find('.tips.cf').each(function(i, elm) {
    console.log($(this).text()) // for testing do text() 
});

$('#browse-results').children().find('.tips').each(function(i, elm) {
    console.log($(this).text())
});
I've tried many more

The output is just the values of the first <ul>.

tip11
tip12
tip13

Please note guys that this is just a snippet example with the same structure of what I'm trying to parse.

I spent nearly 2 hours on this, and I can't find a way to do it.

Timothy Strimple

Try this:

var cheerio = require('cheerio');

var html = '<div id="browse-results"> \
    <ul class="tips cf"> \
        <li>tip11</li> \
        <li>tip12</li> \
        <li>tip13</li> \
    </ul> \
    <ul class="tips cf"> \
        <li>tip21</li> \
        <li>tip22</li> \
        <li>tip23</li> \
    </ul> \
    <ul class="tips cf"> \
        <li>tip31</li> \
        <li>tip32</li> \
        <li>tip33</li> \
    </ul> \
    <ul class="tips cf"> \
        <li>tip41</li> \
        <li>tip42</li> \
        <li>tip43</li> \
    </ul> \
</div>';

var $ = cheerio.load(html);

$('#browse-results li').each(function(i, elm) {
    console.log($(this).text()) // for testing do text() 
});

This will select all li elements which are decedents of #browse-results.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

CheerioJS,通过具有相同类名的<ul>循环

来自分类Dev

Looping through Google Sheet

来自分类Dev

Show / Hide divs individually with same class name

来自分类Dev

Looping through JSON format in javascript

来自分类Dev

带有 has_many、through、class_name 和 where 子句的 Rails 模型关联

来自分类Dev

Django表单错误:<ul class =“ errorlist”> <li>语言<ul class =“ errorlist”> <li>输入值列表。</ li> </ ul> </ li> </ ul>

来自分类Dev

jQuery move/append multiple items with same class name, inside parent div only

来自分类Dev

ul Class线型无法正常工作

来自分类Dev

How can we skip a record while looping through an array in ReactJS

来自分类Dev

cheeriojs-如何遍历每个对象?

来自分类Dev

cheeriojs解析问题:找不到<a>标记?

来自分类Dev

如何使用BeautifulSoup提取<ul class = ...>下的<li>下的值

来自分类Dev

Testing Functions with the same name in GO

来自分类Dev

Two Operations with the same name in the Service

来自分类Dev

Using lodash is there a way to decrement i and start looping through array from the top?

来自分类Dev

Python/Selenium - find_elements_by_class_name with long class name

来自分类Dev

外键class_name

来自分类Dev

外键class_name

来自分类Dev

Move inside UL and change LI class if has .has_children class

来自分类Dev

Have the same Java file name as the project name in intelliJ

来自分类Dev

Selecting records with the same name give duplicate results

来自分类Dev

BASH : Sum size of same name directories

来自分类Dev

Laravel 4 - JOIN - Same column name

来自分类Dev

Replace column if the same column name R

来自分类Dev

RestKit XML Mapping with same element and attribute name

来自分类Dev

Set element name same as RootElement attribute

来自分类Dev

How to move directory into a directory with the same name?

来自分类Dev

How to obtain the JVM thread name/id through the known PID/TID

来自分类Dev

div id =“ textcontent” h1里面有我的ul class =“ navBar”

Related 相关文章

  1. 1

    CheerioJS,通过具有相同类名的<ul>循环

  2. 2

    Looping through Google Sheet

  3. 3

    Show / Hide divs individually with same class name

  4. 4

    Looping through JSON format in javascript

  5. 5

    带有 has_many、through、class_name 和 where 子句的 Rails 模型关联

  6. 6

    Django表单错误:<ul class =“ errorlist”> <li>语言<ul class =“ errorlist”> <li>输入值列表。</ li> </ ul> </ li> </ ul>

  7. 7

    jQuery move/append multiple items with same class name, inside parent div only

  8. 8

    ul Class线型无法正常工作

  9. 9

    How can we skip a record while looping through an array in ReactJS

  10. 10

    cheeriojs-如何遍历每个对象?

  11. 11

    cheeriojs解析问题:找不到<a>标记?

  12. 12

    如何使用BeautifulSoup提取<ul class = ...>下的<li>下的值

  13. 13

    Testing Functions with the same name in GO

  14. 14

    Two Operations with the same name in the Service

  15. 15

    Using lodash is there a way to decrement i and start looping through array from the top?

  16. 16

    Python/Selenium - find_elements_by_class_name with long class name

  17. 17

    外键class_name

  18. 18

    外键class_name

  19. 19

    Move inside UL and change LI class if has .has_children class

  20. 20

    Have the same Java file name as the project name in intelliJ

  21. 21

    Selecting records with the same name give duplicate results

  22. 22

    BASH : Sum size of same name directories

  23. 23

    Laravel 4 - JOIN - Same column name

  24. 24

    Replace column if the same column name R

  25. 25

    RestKit XML Mapping with same element and attribute name

  26. 26

    Set element name same as RootElement attribute

  27. 27

    How to move directory into a directory with the same name?

  28. 28

    How to obtain the JVM thread name/id through the known PID/TID

  29. 29

    div id =“ textcontent” h1里面有我的ul class =“ navBar”

热门标签

归档