인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

Simple's profile image
Simple

asked

Web Crawling with Node.js

3-5. Bonus: Combining CSS Selectors

선택자 관련 질문 드립니다.

Written on

·

195

0

안녕하세요.  선택자 관련해서 잘 이해가 안가는점이 있어 문의드립니다.

 

크롬 console 에서 $$('tr.abc td')[1].textContent 명령어를 칠 경우 100이라는 숫자를 뽑아옵니다.

 

소스 코드 상에서 아래와 같이 여러 방법을 사용해봤는데요.

100 이라는 데이터를 못가져오더라고요. 관련해서 어떻게 접근해야 100 이라는 데이터를 가져올 수 있을까요?

 

source code

const text = await page.evaluate(() => {
const num = document.querySelector('tr.abc td')[1];
return num.textContent;
});

const text = await page.evaluate(() => {
const num = document.querySelectorAll('tr.abc td')[1];
return num;
});

const text = await page.evaluate(() => {
const num = document.querySelector('tr.abc td');
return num;
});

 

HTML 태그입니다.

<tr class="abc">

  <td class="text">숫자</td>

  <td>100</td>

   ...

   ...

</tr>

 

 

javascriptnodejs웹-크롤링puppeteer

Answer 1

0

zerocho님의 프로필 이미지
zerocho
Instructor

document.querySelectorAll('tr.abc td')[1].textContent;
textContent를 왜 안 붙이셨나요?
Simple님의 프로필 이미지
Simple
Questioner

그렇네요.. textContent 를 붙이니까 값이 나오네요. 아래 예제처럼 score 에 textContent 를 붙이는 식으로 될줄알았는데 크롬 console 에 나온거랑 똑같이 해야하는거였네요. 

답변 감사합니다.

 

const text = await page.evaluate(() => {
// const score = document.querySelector('.score.score_left .star_score');
const score = document.querySelector('.score.score_left');
if (score) {
return score.textContent;
};
Simple's profile image
Simple

asked

Ask a question