This article mainly introduces how to save a webpage to a local python instance. For more information about how to save a webpage to a local machine, see the following python example.
The code is as follows:
# Coding = UTF-8
_ Auther _ = 'xianbao'
Import urllib
Import OS
Def reporthook (blocks_read, block_size, total_size ):
If not blocks_read:
Print 'open connection'
Return
If total_size <0:
Print "% d reading (% dbytes completed)" % (blocks_read, blocks_read * block_size)
Else:
Amout_read = block_size * blocks_read
Print '% d reading, % d/% d' % (blocks_read, amout_read, total_size)
Return
Try:
Filename, msg = urllib. urlretrieve ('http: // www.jb51.net/', reporthook = reporthook)
Print
Print 'File: ', filename
Print 'header file is'
Print msg
Print 'file address before deletion: ', OS. path. exists (filename)
Finally:
Urllib. urlcleanup ()
Print 'file still exists: ', OS. path. exists (filename)