Python Learning Urllib Module---for sending network requests, getting data (2)

Source: Internet
Author: User

followed by the last content.

Let's start with the Split () method: It slices the string by specifying a delimiter, and if the parameter num has a specified value, it separates only the NUM substring (a list that splits a string into a number of strings)

Syntax:str. Split(str="", num=string. Count(str)).

< span class= "str" >< Span class= "PLN" >                                parameter: str delimiter, default is a space. Num Split count

< span class= "str" >< Span class= "PLN" >                               return value: Returns the segmented list of strings

< span class= "str" >< Span class= "PLN" >                               For example: you need to take each word in an English sentence out of the box, and you can split it.

< span class= "str" >< Span class= "PLN" >                                        such as: A= ' I am a new student '

< span class= "str" >< Span class= "PLN" >                                             A.split ()  

< span class= "str" >< Span class= "PLN" >                                             Split splits the string into spaces, all of which are new strings, and eventually return a list of strings,

< span class= "str" >< Span class= "PLN" >                                             Results [' I ', ' am ', ' a ', ' new ', ' student ']

< span class= "str" >< Span class= "PLN" >                                             In fact, the split () method is very similar to the delimiter in Excel.

< span class= "str" >< Span class= "PLN" >                                it corresponds to the method join () that joins the list string, which is to concatenate all the strings in the list into a string

                                such as: s= ', '    lis= [' I ', ' am ', ' A ', ' new ', ' Student ']   result=s.jion (LIS)      print result          get results for ' i,am,a,new,student '

< span class= "str" >< Span class= "PLN" >                                Press, connect list           

< span class= "str" >< Span class= "PLN" >                               list can traverse the elements in the LIS with for...in.    for I in rang (1,10) here rang (1,10) is a list  [1,2,3,4,5,6,7,8,9] 

< span class= "str" >< Span class= "PLN" >                               I=rang (1,10) =[1,2,3,4,5,6,7,8,9],  for i in I: And for i  in Rang (1,10): The effect is the same.

< span class= "str" >< Span class= "PLN" >                               explain how to manipulate list List

< span class= "str" >< Span class= "PLN" >                               1 "Access to elements in list    as above print lis[0] will output ' I '

2 "Modify the list of elements such as modify the above lis,lis[4]= ' teacher ', the output of the LIS when the last element was changed to ' teacher '

3 "list add element with Append method such as Lis.append (2016)

4 "Delete list element with Del method, e.g. Del[4]

Ah, said so much, in fact, the feeling is not what the use of rice, in fact, these are the basic knowledge, today, I review it again, because crawling the content of the network is often to manipulate the list drop. With this in view, you can continue learning to capture the province/municipality number today.

Yesterday's study is how to get a network resource information, which is similar to the file operation, is open, and then read, decode, split into a list of the steps. You can use this code directly

B=urllib.request.urlopen (' Http://m.weather.com.cn/data5/city.xml '). Read (). Decode (). Spilt (', ')

This allows you to get a list of B, which is what the above print prints. Analysis, we have to get the data 01, 02, we have to manipulate this B-list

Here's a list of the B's to be manipulated, try

Print (b[0]) Look, is it printed out ' 01| Beijing '? To continue the analysis, at this time b[0] is also a string, it will be divided according to |, you can draw 01, so the operation is as follows, should be b[0].spilt (' | ') , the split data should be [01, ' Beijing '], at this time again value: B[0].split (' | ') [0]? The idea is this, see if it is?

One is to do so, and the same is the same as the loop, so you have to use the for I in list

For i in B: #i =b[0],b[1],b[2] ...

Code=i.split (' | ') [0]

web= ' http://m.weather.com.cn/data5/city%s '

Web1=web%code

Print (WEB1)

See if I can print out the data for the second interface? Of course, if you feel too much, you can only play two or three provinces to see. For I in B[:2]

Well, it's a little late, and tomorrow, September 1, 2016.

Python Learning Urllib Module---for sending network requests, getting data (2)

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.