이 질문은 확실히 여러 번 요청되었지만 모든 곳을 살펴 보았고 대답 중 어느 것도 나를 위해 일하지 않았습니다.
그래서 다음 Div가 있습니다.
<div class="dataTables_info" id="dt-card-entry_info" role="status" aria-live="polite">
Showing 1 to 20 of 761,871 entries
<span class="select-info">
<span class="select-item">
1 row selected
</span>
<span class="select-item">
</span>
<span class="select-item">
</span>
</span>
</div>
상위 div에 텍스트를 가져 오려고합니다 : 761,871 개 항목 중 1 ~ 20 개 표시
나는 시도했다 :
const text = await page.$eval('div#dt-card-entry_info.dataTables_info', el => el.textContent)
그리고 또한
const text = await page.evaluate(() => {
const el = document.querySelector('#dt-card-entry_info')
return el.innerText
})
브라우저 콘솔에서 다음과 같이 작동합니다.
$('#dt-card-entry_info').text()
그리고 이것 :
$('#dt-card-entry_info')[0].innerText
아니면 이거:
$('#dt-card-entry_info')[0].textContent
당신이 사용할 수있는
텍스트 내용을 원하므로 다음을 사용하십시오.
var res = document.getElementById('dt-card-entry_info').textContent;
다음과 같이 방법을 사용할 수 있습니다.
const text = await page.evaluate(() => {
const el = document.getElementById('dt-card-entry_info');
return el.textContent;
})
const
def 의 await pageEval이 마음에 들지 않으므로 eval 범위 밖에서 변경합니다.
이는 pageEval이 promise이기 때문에 문자열 콘텐츠의 promise를 반환해야합니다. 여기에서 더 읽기
let text = '';
await page.evaluate(() => {
const el = document.getElementById('dt-card-entry_info');
text = el.textContent;
})
console.log(text);
여기서 일할 수 있습니다 : https://jsfiddle.net/9s4zxvLk/
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다