python3 urllib.request.Request的用法

import urllib.request
import urllib.parse

url = 'http://127.0.0.1:8000/api/login/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
headers['Host'] = 'httpbin.org'
dict = {'user': 'admin','pwd': '12345',}

data = urllib.parse.urlencode(dict).encode('utf-8')
#data参数如果要传必须传bytes(字节流)类型的,如果是一个字典,先用urllib.parse.urlencode()编码。
request = urllib.request.Request(url = url,data = data,headers = headers,method = 'POST')

response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')

print(html)

 

你可能感兴趣的:(python基础)