Home  >  Article  >  Backend Development  >  javascript - Download webpage with js?

javascript - Download webpage with js?

WBOY
WBOYOriginal
2016-10-10 11:56:121508browse

It’s easy to download web pages with php

<code>    
    $url='https://segmentfault.com/q/1010000000149454';
    $data=file_get_contents($url); 
    $target=fopen('/tmp/test.html',"w");
    fwrite($target, $data);    
    fclose($target);
    </code>

$url is downloaded locally and saved as /tmp/test.html file.

1. How to save the current page
Enter in the browser
https://segmentfault.com/q/10...
Open this webpage
Then, in the console
var content = document.getElementsByTagName("html") [0].outerHTML;
content is the content of this webpage. How can I save it in the /tmp/test.html file using javascript?
document.execCommand('Saveas',false,'/tmp/test.htm');
false
2. How to save any web page?
This can be done using the php above, what about javascript?

Reply content:

It’s easy to download web pages with php

<code>    
    $url='https://segmentfault.com/q/1010000000149454';
    $data=file_get_contents($url); 
    $target=fopen('/tmp/test.html',"w");
    fwrite($target, $data);    
    fclose($target);
    </code>

$url is downloaded locally and saved as /tmp/test.html file.

1. How to save the current page
Enter in the browser
https://segmentfault.com/q/10...
Open this webpage
Then, in the console
var content = document.getElementsByTagName("html") [0].outerHTML;
content is the content of this webpage. How can I save it in the /tmp/test.html file using javascript?
document.execCommand('Saveas',false,'/tmp/test.htm');
false
2. How to save any web page?
This can be done using the php above, what about javascript?

In the browser, your request is difficult to implement due to security factors.
You can consider implementing it in nodejs.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn