문제

my html liks:

<dl class="resume_pro">  
    <dt>    <h3>personal infomation</h3>  </dt>  
    <dd class="pro_lf"> 
        <span class="rt_title">sex:male | age:26 </span>
        <div class="clear"></div> 
        <br>phone:123456789<a href="###" class="send" id="sendsms" style="display:none">send message</a><br>   E-mail:name@abc.com <br>  
    </dd>
    <div class="clear"></div>
</dl>

my parser code :

var $ = cheerio.load(html);
found = $('*:contains("phone:")').last();

the found will get "<dd class="pro_lf"> </dd>"

and then found.text() will get "sex:male | age:26 phone:123456789send message E-mail:name@abc.com "

but how can i get each phone and e-mail?

i wanna to write a code in common use

so i just used $('*:contains("phone:")') to search my infomation, not use tag name or class name

i will loop the element to find out every last node and get content to parser

i need some help.

도움이 되었습니까?

해결책 2

I should use this to loop over each element:

found.contents().each(function() {
.....
});

Then you can use a regex in the loop in order to get the phone number.

다른 팁

There's probably a thousand ways to do this, but here's a succinct way using a regular expression (of which I am no master, but here's my take):

var $ = cheerio.load(html);
found = $('*:contains("phone:")').last();

//Find phone number
var phoneNumber = str.match(/phone\:\d+/)[0].match(/\d+$/);

The match will find the string "phone:123456789" and return it in an array with only one element. Then we split the string where "phone:" appears, leaving the array ["", "123456789"].

To expand on the RegEx /phone\:\d+/:

/                   start of regex
 phone\:            match the string literal, "phone:"
 \d+                match 1 or more digits following "phone:"
/                   end of regex

And for /\d+$/:

/                   start of regex
 \d+                match 1 or more digits
 $                  ...at the end of the string
/                   end of regex

After running this, phoneNumber will be the string "123456789".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top