用Javascript抓取网页内容

打印网页所有h2的内容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Array.from(document.querySelectorAll('h2')).map(x => console.log(x.textContent))
Array.from(document.querySelectorAll('h2')).map(x => console.log(x.textContent))
Array.from(document.querySelectorAll('h2')).map(x => console.log(x.textContent))

document.querySelectorAll
document.querySelectorAll返回类型为
NodeList
NodeList
NodeList
NodeList是不能用map方法的。所以需要用
Array.from
Array.from先把
NodeList
NodeList转换成
array
array,就可以用map对每个元素操作了。

querySelectorAll
querySelectorAll的参数和CSS一样

querySelectorAll
querySelectorAll的输入
选中DOM例子
h2select by tag <h2> </h2>
.cls1.cls2.cls3select by class name <h2 class=”cls1 cls2 cls3″></h2>
#id1select by id <h2 id=”id1″></h2>

除了用也可以用

document.querySelectorAll
document.querySelectorAll也可以用document.getElementsByTagName达到一样效果。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Array.from(document.getElementsByTagName('h2')).map(x => console.log(x.textContent))
Array.from(document.getElementsByTagName('h2')).map(x => console.log(x.textContent))
Array.from(document.getElementsByTagName('h2')).map(x => console.log(x.textContent))

Leave a Comment

Your email address will not be published.