import urllib.request import urllib.parse headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62' } data = { 'kw':'Spider' } reqData = urllib.parse.urlencode(data).encode('utf-8') url = 'https://fanyi.baidu.com/sug' request = urllib.request.Request(url=url,headers=headers,data=reqData) response = urllib.request.urlopen(request) content = response.read().decode('utf-8') print(content)
import json
obj = json.loads(content)
print(obj)
data先urlencode之后还要再encode,保证请求参数是字节
标签:请求,url,request,urllib,content,import,post,data From: https://www.cnblogs.com/sgj191024/p/17737880.html