I. Introduction
() function is used to implement access to the target url.
The function prototype is as follows:
(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
-
url
:: Web site to be opened -
data
: Data submitted by Post -
timeout
: Setting the access timeout for your website
Directly use the module's urlopen() to get the page, the page's data format is of type bytes, which needs to be decoded() and converted to type str.
II. Introduction to function parameters
-
url
Parameter: the location of the target resource in the net. Can be a string representing the URL; can also be an object, for details please jump to -
data
Parameter: data is used to specify additional parameter information in the request to the server (e.g. online translation, online question and answer, etc.), data is None by default, and the request will be sent by GET; when the user gives the data parameter, the request will be sent by POST. -
timeout
: Setting the access timeout for your website -
cafile
、capath
、cadefault
Parameter: HTTP request for implementing trusted CA certificates. (largely rarely used) -
context
Parameters: Enables SSL encrypted transmission. (largely rarely used)
III. Return processing methods in detail
The urlopen return object provides methods:
read() , readline() ,readlines() , fileno() , close() : operate on HTTPResponse type data
-
info()
: Returns an HTTPMessage object representing the header information returned by the remote server -
getcode()
: Returns the Http status code. If it is an http request, 200 request completed successfully; 404 url not found -
geturl()
: return the url of the request
IV. Version differences, notes
Both python2 and python3 have different ways of importing urlrequests.
python2 is this: import urllib2
In python3, urllib is separated into urlrequest and urlerror, so we can just import urlrequest here. from import urlopen
V. Examples
The following program, implements most of the functionality of the urlopen() function, especially the data parameter.
data customization, data format conversion, encoding encode() and decoding decode() of data.
#coding=utf-8 # ''' Online Translation with Yodo Translator ''' import import import json def traslate(words): #Target URL targetURL = "/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, or you can use a list of tuples (as already tried). data = {} data['type'] = 'AUTO' data['i'] = words data['doctype'] = 'json' data['xmlVersion'] = '1.8' data['keyfrom'] = '' data['ue'] = 'UTF-8' data['action'] = 'FY_BY_CLICKBUTTON' data['typoResult'] = 'true' # Convert custom data to standard format data = (data).encode('utf-8') # Send user requests html = (targetURL, data) # Read and decode content rst = ().decode("utf-8") rst_dict = (rst) return rst_dict['translateResult'][0][0]['tgt'] if __name__ == "__main__": print("Enter the letter q for quit.") while True: words = input("Please enter the word or sentence to be looked up:\n") if words == 'q': break result = traslate(words) print("Translation result: %s"%result)
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.