Parsing of Python urlopen of Parameter Code Example

  • 2021-08-17 00:36:51
  • OfStack

1. data parameter

data is optional and requires the bytes () method to convert the parameters to byte-encoded content. If this parameter is passed, the request mode is not GET mode, but POST mode.


import urllib.parse
import urllib.request

data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8')# Use bytes() Method sets the parameter word( Value is hello) To a byte stream ( bytes ), # The number of the method 1 Parameters required str Type, you need to use urllib.parse In the module urlencode() Method converts the parameter dictionary to a string. response = urllib.request.urlopen('http://httpbin.org/post',data = data)
print(response.read())

2. timeout parameter

Used to set the timeout time in seconds. If the set time is exceeded and no response is received, an exception will be thrown. You can set this timeout to control 1 page to skip crawling if it does not respond for a long time.


import socket
import urllib.request
import urllib.error

try:
  response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)# Set the timeout time to 0.1s
except urllib.error.URLError as e:
  if isinstance(e.reason,socket.timeout):
    print('TIME OUT')# If timeout, output TIME OUT

3. Other parameters

context parameter, type must be ssl. SSLContext type.

The cafile and capath parameters specify the CA certificate and its path, respectively, and are useful when requesting an HTTPS link.

The cadefault parameter has been deprecated and its default value is False.


Related articles: