缩短 axios text/html 响应

shorten axios text/html response

我正在向网页发送 Axios 请求以从中抓取一些字符串,但返回的响应很大 html,而我只需要其中的一小部分,有没有以某种方式缩短响应以便我可以保存数据并使请求更快的方法?

const longHtml = await axios.get('https://example.com');
const shortHtml = longHtml.data //get short data here

网络抓取是一种用于从网站检索数据的技术。您获取页面的内容,然后从页面中提取您需要的数据。

这里是一个使用 axios 和 cheerio

的例子
const axios = require("axios")
const cheerio = require("cheerio")

async function fetchHTML(url) {
  const { data } = await axios.get(url)
  return cheerio.load(data)
}
const $ = await fetchHTML("https://example.com")

// Print the full HTML
console.log(`Site HTML: ${$.html()}\n\n`)

// Print some specific page content
console.log(`First h1 tag: ${$('h1').text()}`)

如果我明白你想做什么,你不想在找到你想要的数据时停止请求,你可以使用 htmlparser2 并从 axios 向它提供流,然后注册监听器和当你得到你需要的元素时,你可以结束流。