#以下介绍是基于Python3.4.3
I. INTRODUCTION
The Urllib.request.urlopen () function is used to implement access to the destination URL.
The function prototype is as follows:urllib.request. Urlopen (url, data=none, [timeout, ]*, cafile=none, capath=none, cadefault=false, context=none)
The function is defined as follows:
defUrlopen (URL, Data=none, timeout=Socket._global_default_timeout,*, Cafile=none, Capath=none, Cadefault=false, context=None):Global_openerifCafileorCapathorCadefault:ifContext is notNone:RaiseValueError ("You can ' t pass both context and any of cafile, Capath, and" "Cadefault" ) if not_have_ssl:RaiseValueError ('SSL Support not available') Context=Ssl.create_default_context (SSL. Purpose.server_auth, Cafile=Cafile, Capath=capath) Https_handler= Httpshandler (context=context) Opener=Build_opener (Https_handler)elifContext:https_handler= Httpshandler (context=context) Opener=Build_opener (Https_handler)elif_opener isNone: _opener= opener =Build_opener ()Else: Opener=_openerreturnOpener.open (URL, data, timeout)
Two. function parameter introduction
<1>URL parameter: The location of the target resource in the network. Can be a string representing a URL (such as: http://www.xxxx.com/), or a Urllib.request object, in detail please jump
<2> Data parameter: Data is used to indicate additional information sent to the server request (e.g. online translator, online answer, etc.). HTTP is a Python implementation of many network communications HTTP, HTTPS, FTP and other protocols, the only one using the data parameter, that is, only open the HTTP URL, the custom data parameter will be useful. In addition, the official API manual explains that:
<2.1> data must be a Byte data object (bytes object in Python)
<2.2>data must conform to the standard application/x-www-form-urlencoded format, how to get the data of this standard structure? Use Urllib.parse.urlencode () to convert the custom data to
The standard format in which the function can receive parameter types is mapping object in Pyhon (key/value pairs, such as dict) or a sequence of two-element tuples (the element is a list of tuple).
<2.3>data can also be an iterative object, in which case the conten-length in the response object needs to be configured to indicate the size of the data.
<2.4>Data defaults to none, at which point the request is sent in a Get mode, and when the user gives the data parameter, the request is sent as a post.
<3>cafile, Capath, cadefault parameters: an HTTP request to implement a trusted CA certificate. (mostly rarely used)
<4>context parameter: Implement SSL encrypted transmission. (mostly rarely used)
Three. Give me a chestnut.
The following program implements most of the functions of the Urlopen () function, especially the data parameter. The data is custom-converted, encoded encode () and decoded decode ().
#Coding=utf-8#Python3.4.3 os:w7-32" "use Youdao Translator for online translation" "Importurllib.requestImportUrllib.parseImportJSONdeftraslate (words):#Destination URLTargetUrl ="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc& Sessionfrom=null" #user-defined form, words represents what the user wants to translate. The dict type is used here, and a list of tuples (already tried) can be used. data ={} data['type'] ='AUTO'data['I'] =words data['DOCTYPE'] ='JSON'data['xmlversion'] ='1.8'data['Keyfrom'] ='Fanyi.web'data['UE'] ='UTF-8'data['Action'] ='Fy_by_clickbutton'data['Typoresult'] ='true' #Convert custom data to standard formatdata = Urllib.parse.urlencode (data). Encode ('Utf-8') #Send user RequestHTML =Urllib.request.urlopen (targeturl, data)#Read and decode contentrst = Html.read (). Decode ("Utf-8") Rst_dict=json.loads (RST)returnrst_dict['Translateresult'][0][0]['TGT']if __name__=="__main__": Print("entering the letter Q means exiting") whileTrue:words= Input ("Please enter a word or sentence to query: \ n") ifWords = ='Q': Breakresult=traslate (words)Print("The translation result is:%s"%result)
~ ~ ~ To be improved, I hope you put forward valuable advice, study together.
=======================================================================================================
Reference source: "Little Turtle 0 Basic Beginner Python" This video is very good, humorous, and detailed.
Official Document: https://docs.python.org/3/
Python in Urlopen () Introduction