With this simple script you can download an html page.
The script is written in python and runs from the command line. The operation is very simple to write the shell: python nomefile.py http://pagina.html nomeFileDaSalvare save the script in the current page.
The script is as follows:
import urllib;
import sys;
if len (sys.argv) \u0026lt;2:
print "missing pages and the address of the file name
..."; exit (0);
if len (sys.argv) \u0026lt;3:
print "Missing file name
..."; exit (0);
urllib.urlretrieve (sys.argv [1], sys.argv [2]);
print" Dowload page: "+ sys.argv [1] + "with the name:" + sys.argv [2] + "completed";
With the first two lines indicate the two packages that we need, we urllib serves to make the download and read sys line arguments command. This control
if len (sys.argv) occurs if the array of arguments (sys.args) and specify the url of the page to download and the second checks if len (sys.argv) must ensure also the name of the file saved in the current folder. If you are missing a parameter displays an error message and terminates the program. This statement
: urllib.urlretrieve (sys.argv [1], sys.argv [2]); is the most important because it is one that cares to download and save the page.
Finally a message is printed stating that the download is complete. With few
lines of code we wrote a script strong enough, that if we tried to write in another language such as Java or C + + would have been much more verbose.
0 comments:
Post a Comment