简体   繁体   English

是否可以使用我的本地网络/浏览器从给定网页下载 html 文件,就像我使用 javascript 或 nodejs 自己下载一样?

[英]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.

相关问题 是否可以从服务器托管HTML和Javascript,但是可以通过我的本地网络发送SOAP请求吗? - Is it possible to host HTML and Javascript from server, but have it send SOAP request over my local network? 如何通过浏览器下载本地网络文件 - how to download local network file via browser 需要从网页下载文件到本地驱动器 - Need to download a file from webpage to local drive 如何在浏览器上运行从 Github 下载的 Angular nodejs 和 MongoDB crud 项目? - How do I run an Angular nodejs and MongoDB crud project I have downloaded from Github on my browser? 是否可以使用Javascript从FTP下载文件? - Is it possible to download file from FTP using Javascript? 如何使用javascript下载网页的整个HTML? - How to download entire HTML of a webpage using javascript? 从CDN在浏览器中下载文件并设置下载的文件名 - Download file in browser from CDN and set downloaded file name 来自浏览器的GET请求可将文件下载到本地,但XMLHttpRequest Javascript脚本无法下载文件 - GET request from browser works to download file to local but XMLHttpRequest Javascript script does not download file 下载的HTML文件是否可以获取本地目录的目录列表? - Is it possible for a downloaded HTML file to get a directory listing of local directories? 如何使用 javascript 将包含已填写表单的 html 网页下载为 HTML 文件 - How to download an html webpage containing a filled form as an HTML file using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM