Python: Standard Library - supplement on the urlopen

Processing get request does not pass Data, compared get request

import urllib
from urllib.request import urlopen
from urllib.parse import urlencode

= url ' http://www.xxx.com/login '
Data = { "username": "ADMIN", "password": 123456}
REQ_DATA = urlencode (Data) # The type of request dictionary data into encoded url
res = urlopen (url + '?' + req_data) # urlopen method spliced by the access URL
RES = res.read (). decode () # read () method returns the read content data, decode the data bytes are returned format conversion It is str
Print (RES)

Post processing request, if the Data transfer, was post requests

import urllib
from urllib.request import Request
from urllib.parse import urlencode

= url ' http://www.xxx.com/login '
data = { "username": "ADMIN", "password": 123456}
data = urlencode (data) # The type of request dictionary url encoded data into
data = data.encode ( 'ascii') # url encoding type of the requested data into bytes type
req_data = request (url, data) # url and the data processing request as a request object, calling for urlopen
with urlopen (req_data) as RES:
RES = res.read () decode () # read () method returns the read content data, decode the data is switched back to the format bytes str.

print(res)

Guess you like

Origin blog.csdn.net/weixin_44523387/article/details/92165719