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 returned data is a document.
  • You can use all the methods of document like querySelector on the returned data object.
Posted on 2023-09-01