#测试接口:https://httpbin.org/post
from urllib import parse,request
url = 'https://httpbin.org/post'
#表单数据
fordata = {
'name':'红红火火',
'age':18,
'gender':'男'
}
#先使用urlencode将参数转为url编码格式的字符串,
# 然后在使用encode()方法将字符串转为bytes类型的参数
formdata = parse.urlencode(fordata).encode('utf-8')
red_header = {
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
req = request.Request(url,headers=red_header,data=formdata)
response = request.urlopen(req)
print(response.status)
print(response.read().decode('utf-8'))
有道
#有道翻译的post请求的url地址:
#http://fanyi.youdao.com/translate_o?
# smartresult=dict&smartresult=rule
# http://fanyi.youdao.com/translate?
# smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null
from urllib import parse,request
import json
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
formdata = {
'i': '我的祖国',
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICKBUTTION',
'typoResult': 'false',
}
formdata = parse.urlencode(formdata).encode('utf-8')
req_header = {
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
}
req = request.Request(url,headers=req_header,data=formdata)
response = request.urlopen(req)
print(response.status)
# print(response.read().decode('utf-8'))
json_str = response.read().decode('utf-8')
print(json_str)
# json.loads():将json字符串,转换为python的数据类型;
# json类型的数据(对象和数组)对象->dict 数组->list
# json.load()
# json.dumps()
# json.dump()
"""
{
"type":
"ZH_CN2EN",
"errorCode":0,
"elapsedTime":23,
"translateResult":
[
[
{"src":"我的祖国","tgt":"My motherland"}
]
]
}
"""
#{"name":"xxx","age":18,"info":["1","2","3"]}
data = json.loads(json_str)
print(type(data))
result = data['translateResult'][0][0]['tgt']
print(result)
网友评论