Python3 urllib.request.urlopen Example

urllib.request.urlopen() method is used to access the target url. This article will tell you how to use it with examples.

1. urllib.request.urlopen() Function Definition.

  1. urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None).
  2. url : The location of the target resource in the network. It can be a string representing the URL (for example: http://www.test-abc.com/), it can also be a urllib.request object.
  3. data : data is used to indicate the additional parameter information in the request sent to the server. The default value of data is None, and the request is sent in get mode. When the user specify the data parameter, the request is sent in post mode instead.
  4. timeout : Set access timeout for the website request connection.
  5. cafile, capath, cadefault : Used to implement trusted CA certificates HTTP requests.
  6. context : Implement SSL encrypted transmission.

2.urllib.request.urlopen() Return Object Method Introduction.

The object returned by the urlopen() method provides the following methods.

  1. read(), readline(), readlines(), fileno(), close() : Read data ( text or binary ) from returned HTTPResponse object.
  2. info() : Returns the HTTPMessage object, which represents the header information returned by remote server.
  3. getcode() : Returns the HTTP status code. If it is an HTTP request, then 200 means the request is completed successfully, 404 means URL page is not found.
  4. geturl() : Returns the request url.

3. Version Differences.

The way to import urllib.request.urlopen method is different in python2 and python3.

  1. In python2, it is import urllib2.
  2. In python3, urllib is divided into urlrequest and urlerror. Here we just need to import urlrequest.
    from urllib.request import urlopen

4. Use urllib.request.urlopen() Method Example.

The following example implements most features of the urlopen() function, especially the data parameter. It shows how to make data customization, data format conversion, data encoding() and decoding().

# Import the python modules which is needed in this example.
import urllib.request
import urllib.parse
import json

# The function will call an online url to translate the provided words.
def traslate(words):
    #The target URL that provide words translation feature.
    targetURL = "http://fanyi.test-abc.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"
    
    # User defined form data. Words represent the content to be translated. The dictionary type is used here. You can also use a tuple list.
    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 format
    data = urllib.parse.urlencode(data).encode('utf-8')

    # Send user request.
    response = urllib.request.urlopen(targetURL, data)

    # Read and decode server returned content.
    result = response.read().decode("utf-8")
    
    # Convert the returned content to json format.
    rstult_dict = json.loads(result)

    # Return the translate result string.
    return rstult_dict['translateResult'][0][0]['tgt']

# The main function.
if __name__ == "__main__":
    print("Enter the letter Q to exit")

    while True:
        words = input("Please input the word or sentence you want to translate:\n")
        if words == 'q':
            break
        result = traslate(words)
        print("The result of translation is :%s"%result)
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Clicky
0
Would love your thoughts, please comment.x
()
x