Detailed explanation of Python Library Network (1)-webpage capturing

Source: Internet
Author: User

A simple example of Python embedding is just written (almost enough for now ~), Next, let's look at the actual things. Without these applications, the previous embedding will be meaningless. When writing other embedded parts later, you don't have to understand all the functions at once, right ~

 

Okay. Let's see what I think is the most interesting part of the python library, that is, how Python operates web pages.

 

This article briefly explains how to download a webpage through a web site, on the premise of being able to access the Internet.

Here, I want to store the web pages under the test.html file.

 

Only two lines of code are required to download the webpage:

 

# Note that the current version of python3.1 has changed a lot. In the past, many codes were unavailable and need to be modified a little.

Import urllib. Request

Urllib. Request. urlretrieve ('HTTP: // www.163.com', 'test.html ') # How can I solve this problem if you use double quotation marks? Is there a difference between single quotes and double quotes?

 

Haha, I don't think it's too simple, but I still like the implementation of the code below (I didn't add an exception, if it's a bit formal code, add some code to handle exceptions ):

Import urllib
Import urllib. Request

 

Url = urllib. Request. urlopen ("http://www.163.com ")
File = open ("test.html", 'wb') # It's 'wb', not 'W'

While (1 ):
Line = URL. Readline ()
If Len (line) = 0:
Break
File. Write (line)
 

Oh, do you really feel boring about downloading a webpage ~

OK. Next I will explain how to parse the downloaded webpage so that the downloaded webpage will be useful ~ Pai_^

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.