2017-05-17 2 views
0

내 질문 게시 누군가가 나를 도울 수 있기를 바랍니다. 봇에 대한 속성을 가진 모든 링크를 잡으려고합니다. cheerio를 사용하여 url을 요청하고 나에게 HTML을 가져옵니다. 문자열로 된 페이지. cheerio - 복수 클래스 선택

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <title>test</title> 
 
    </head> 
 
    <body> 
 
    <a href="google.fr" class="test"></a> 
 
    <a href="yahoo.com" class="test"></a> 
 
    <a href="amazon.fr" class="test"></a> 
 
    <a href="linux.org" class="test"></a> 
 
    <a href="facebook.com" class="no_select"></a> 
 
    <a href="twitter.com" class="no_select"></a> 
 
    </body> 
 
</html>

내가이

const cheerio = require('cheerio'); 
 
const page = `<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>test</title> 
 
</head> 
 
<body> 
 
<a href="google.fr" class="test"></a> 
 
<a href="yahoo.com" class="test"></a> 
 
<a href="amazon.fr" class="test"></a> 
 
<a href="linux.org" class="test"></a> 
 
<a href="facebook.com" class="no_select"></a> 
 
<a href="twitter.com" class="no_select"></a> 
 
</body> 
 
</html>` 
 
const $ = cheerio.load(page) 
 
const links = $('.test').each((index, elem) =>{ 
 
\t console.log(elem); 
 
}); 
 
console.log(links);

처럼하지만 큰 성공을하지 않고 일을 시도했다.

각 요소가 href 속성 인 배열을 검색하는 솔루션을 찾고 있습니다. cheerio를 사용하는 테스트 클래스가있는 링크. 내가 마지막으로 솔루션

const cheerio = require('cheerio'); 
 
const page = `<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>test</title> 
 
</head> 
 
<body> 
 
<a href="google.fr" class="test"></a> 
 
<a href="yahoo.com" class="test"></a> 
 
<a href="amazon.fr" class="test"></a> 
 
<a href="linux.org" class="test"></a> 
 
<a href="facebook.com" class="no_select"></a> 
 
<a href="twitter.com" class="no_select"></a> 
 
</body> 
 
</html>` 
 
const $ = cheerio.load(page) 
 
const links = $('.test').each((index, elem) =>{ 
 
\t console.log(elem.attribs.href); 
 
});
하고 disturbtion 죄송 작품의를 찾을 수

+0

방금 ​​게시물을 편집했습니다. =) –

답변

0

도움이있어 대한

감사합니다 =).