requests the library, concise tutorial (python)

Requests for the python library of external libraries, man of few words, then drive directly do it!

The first is to install the library requests, it simply does not say.

File, the introduction of requests library:

import requests

Let's look at a simple example:

# One of the easiest reptiles small program, just to get to the end of the html source file

Requests Import 
URL1 = "https://live.500.com/" the ISO-8859-1 #
URL2 = "https://www.baidu.com/" the ISO-8859-1 #
URL3 = "HTTPS: // Study .163.com / Course, / introduction.htm? courseId # = 1,209,448,892 / courseDetail? the Tab = 1 "# UTF-8
URL4 =" http://www.cwl.gov.cn/kjxx/ssq/kjgg/ "# ISO -8859-1
Re = requests.get (URL3)
IF re.status_code == 200: # If the request succeeds, it returns a status code is 200.
# Indicates that the request is successful, you can continue to go below
# print (re.text) # may be garbled because the encoding issue
# print (re.encoding) # get-8 character encoding or ISO-UTF 8859-1
IF Re. encoding == "ISO-8859-1": # If this is coded, it should be the character set gb2312, we must turn about code, otherwise there will be Chinese garbled              
html = re.text.encode ( "ISO-8859-1 ") .decode (" GBK ") # encoded first, and then decoding
the else:
HTML = re.
Print (HTML)
the else: # request was unsuccessful.
print ( "access fails, try another URL bar!")

#_________________________________________________________________________________________
re.text # returns the character type 
re.content # returns a byte type, for this type of picture, ah, ah sound, video, etc.
The above example is a get request requests issued library, a post request may be transmitted: 
Re = requests.post (URL3, Data = { "Key": "value"}) with a parameter dictionary to #.
There are several http request as follows:
re = requests.put('http://www.baidu.com', data = {'key':'value'}) re = requests.delete('baidu.com') re = requests.head('www.baidu.com') re = requests.options('http://www.sina.com.cn')

未完待续...
 
 
 

 

Guess you like

Origin www.cnblogs.com/yiyea/p/11441107.html