[英]Is it possible to download an html file from a given webpage using my local network/browser as if I downloaded it myself with javascript or nodejs?
I'm a bit new to javascriipt/nodejs and its packages.我对 javascriipt/nodejs 及其软件包有点陌生。 Is it possible to download a file using my local browser or network?
是否可以使用我的本地浏览器或网络下载文件? Whenever I look up scraping html files or downloading them, it is always done through a separate package and their server doing a request to a given url.
每当我查找抓取 html 文件或下载它们时,总是通过单独的 package 和他们的服务器向给定的 url 发出请求。 How do I make my own computer download a html file as if I did right click save as on a google chrome webpage without running into any server/security issues and errors with javascript?
如何让我自己的计算机下载 html 文件,就像我在 google chrome 网页上右键单击另存为一样,而不会遇到任何服务器/安全问题和 javascript 错误?
Fetching a document over HTTP(S) in Node is definitely possible, although not as simple as some other languages.在 Node 中通过 HTTP(S) 获取文档绝对是可能的,尽管不像其他一些语言那么简单。 Here's the basic structure:
这是基本结构:
const https = require(`https`); // use http if it's an http url;
https.get(URLString, res => {
const buffers = [];
res.on(`data`, data => buffers.push(data));
res.on(`end`, ()=>{
const data = Buffer.concat(buffers);
/*
from here you can do what you want with the data. You can write it to a file
with fs, you can console.log it using data.toString(), etc.
*/
});
})
Edit: I think I missed the main question you had, give me a sec to add that.编辑:我想我错过了你的主要问题,请给我一点时间来补充。
Edit 2: If you're comfortable with doing the above, the way you access a website the same way as your browser is to open up the developer tools (F12 on Chrome) go to the network tab, find the request that the browser has made, and then using http
( s
) .get(url, options, callback)
, set the exact same headers in the options
that you see in your browser.编辑 2:如果您对上述操作感到满意,您访问网站的方式与浏览器相同,即打开开发人员工具(Chrome 上的 F12)go 到网络选项卡,找到浏览器的请求制作,然后使用
http
( s
) .get(url, options, callback)
,在您在浏览器中看到的options
中设置完全相同的标题。 Most of the time you won't need all of them, all you'll need is the authentication/session cookie.大多数时候,您不需要所有这些,您只需要身份验证/会话 cookie。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.