Python Learning Urllib Module---used to send network requests, get data

Source: Internet
Author: User

1.urllib

Urllib is part of the Python standard library and contains a urllib.request,urllib.error,urllib.parse,urlli.robotparser of four sub-modules.

(1) Urllib.request usage

1) Urlopen function: Used to open a URL (urlopen returns a class file object that can be manipulated like a file)

Example: Import urllib.request

Web=urllib.request.urlopen (' http://www.baidu.com ')

Content=web.read ()

Print (Content.decode ())

Explain, here with Decode () decoding, because at that time to try to crawl the Chinese Weather Network city number, found that the crawl display is hexadecimal encoding. Now oneself or small rookie one, as to the University of knowledge almost also did not remember how much, asked the degree Niang, churn out this, for this also to deliberately check the difference between Unicode and other codes such as UTF-8, Unicode is the computer's code, It is a character encoding scheme developed by international organizations that can accommodate all the words and symbols of the world. UTF-8 is characterized by the use of different length of the characters of different ranges of coding, compared to Unicode, the English aspect of the space is less, space-saving, broadband. As for the other differences, not in depth, in the back slowly understand.

Of course, the result here is to print out some page elements of Baidu homepage HTML page.

2) The crawl process of city code

The city code information of the weather network is more complicated, which gives the correspondence relation of city code. The 3 interfaces provided are:

Http://m.weather.com.cn/data5/city.xml gets the province or the municipality's number, such as "01| Beijing, 02| Shanghai" and so on

http://m.weather.com.cn/data5/city%s (province number, such as 01,02). XML, such as City19.xml, gets the number of the two-level area

http://m.weather.com.cn/data5/city%s (two-level number, as in). XML, such as City1901.xml, gets the number of the three-level region

First, get the province or municipality number, the code is as follows:

Remove the printed comment and run the result as follows:

Come here today and continue tomorrow!

Python Learning Urllib Module---used to send network requests, get data

Related Article

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.