python3 urllib.request.Request的用法
阿新 • • 發佈:2018-12-16
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)