The text and pictures in this article come from the network, only for learning, exchange, do not have any commercial purposes, copyright belongs to the original author, if you have any questions, please contact us to deal with

The following article is from Tencent Cloud by Py3Study

(Want to learn Python? Python Learning exchange group: 1039649593, to meet your needs, materials have been uploaded to the group file stream, you can download! There is also a huge amount of new 2020Python learning material.)

Urllib module of the Python standard library

When it comes to networking, the essential mode is urllib.request, which, as its name suggests, opens urls and HTTP protocols

The simplest use of urllib is

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

Url Indicates the url to be opened

Data Indicates the data submitted by Post

Timeout Sets the website access timeout period

Urlopen returns the object providing method

Read (),readline (),readlines(), fileno(), close() : Operations on HTTPResponse data

The geturl() function returns the URL information of response, often used in the case of URL redirection

The info() function returns basic information about response

The getCode () function returns a status code for response. The most common codes are 200 server successfully returned page,404 requested page did not exist, 503 server temporarily unavailable

To test this out, write testurllib.py

#! /usr/bin/env python
# coding: utf-8
__author__ = 'www.py3study.com'
import urllib.request
import time
import platform
import os
def clear() :
    This function is used to clear the screen.
    print(U 'More content, page turn after 3 seconds')
    time.sleep(3)
    OS = platform.system()
    if (OS == u'Windows'):
        os.system('cls')
    else:
        os.system('clear')

def linkbaidu() :
    url = 'https://www.baidu.com'
    try:
        response = urllib.request.urlopen(url, timeout=3)
    except urllib.request.URLError:
        print(U 'Network address error')
        exit()
    with open('baidu.txt'.'w') as fp:
        fp.write(response.read().decode('utf-8'))
    print(U 'geturl information, response,geturl() \n: {}'.format(response.geturl()))
    print(U 'get return code, response.getCode () \n:{}'.format(response.getcode()))
    print(U 'Get the return message, response.info() \n:{}'.format(response.info()))
    print(U 'Retrieved web content to store in current directory baidu. TXT, please check for yourself')

if __name__ == '__main__':
    linkbaidu()
Copy the code

Should see the effectBaidu. TXT is as follows