简体   繁体   English

如何使用 Js 获取 html 页面(元素)

[英]how to fetch a html page(elements) with Js

I am trying to fetch a content of a page.我正在尝试获取页面的内容。 When I inspect and go under the 'Elements' section, I can see the needed data in span tag for example, but when I try to fetch which I do as a get request to the url, I receive different HTML that doesn't contain the same data but less.例如,当我检查并进入“元素”部分时,我可以在 span 标签中看到所需的数据,但是当我尝试获取作为对 url 的获取请求时,我收到了不包含的不同 HTML相同的数据但更少。

This is how I try to fetch my data这就是我尝试获取数据的方式

async function fetchData(url)
{
  const response = await axios.get(url);
  console.log(response.data)
}

What would be the reason of the difference and how could I solve it?差异的原因是什么,我该如何解决?

Should I use different technique?我应该使用不同的技术吗?

Well, indeed, crawlers are supposed to use parsing tools, like Puppeteer library or Cheerio for data-grabbing.嗯,确实,爬虫应该使用解析工具,比如 Puppeteer 库或 Cheerio 来获取数据。

Here are some references:以下是一些参考:

github puppeteer github木偶师

npm puppeteer npm 傀儡师

I've tried to do something like that a few times ago.几次前,我曾尝试做类似的事情。 If you're having troubles - ask for help如果您遇到问题 - 寻求帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM