crawl html page and parse its elements with js axios¶
Install dependencies¶
npm install axios
Code Example¶
In the code below, we crawl a html document, and extract the title text from it.
import axios from 'axios'
export async function CrawlDemo(url) {
const { data } = await axios.get(url, {
responseType: 'document'
})
return data.querySelector('h1').innerText.trim()
}
Notice:
- Pass
responseType: 'document'
option to axios, so that the returneddata
is adocument
. - You can use all the methods of document like
querySelector
on the returneddata
object.
This article is originally created by tooli.top. Please indicate the source when reprinting : https://www.tooli.top/posts/crawl_with_axios
Posted on 2023-09-01
Mail to author