Python implementation save the webpage to a local example-Python tutorial

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.