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