# #post请求百度之详细翻译
# import urllib.request
# import urllib.parse
# url = 'https://fanyi.baidu.com/sug'
# data = {
# 'kw': "spide"
# }
# print(data)
# data = urllib.parse.urlencode(data).encode('utf-8')
# print(data)
# headers = {
# 'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
# }
# #定制请求 # post请求的参数,是不会拼接在url的后面的,而是需要放在请求对象定制的参数中
# request = urllib.request.Request(url=url,data=data,headers=headers)
# # 模拟浏览器向服务器发送请求
# respose = urllib.request.urlopen(request)
# #获取响应数据
# context = respose.read().decode('utf-8')
# context = context
# print(context)
#
# import json
#
# obj = json.loads(context)
# print(obj)
# post 请求方式的参数必须编码 data = urllib.parse.urlencode(data).encode('utf-8')
# 编码之后必须调用encode方法
# 参数是放在请求对象定制的方法中,request = urllib.request.Request(url=url, data=data, headers=headers)
标签:headers,python,request,基础,urllib,url,context,data
From: https://www.cnblogs.com/dhcc/p/18294773