from urllib import request,parse
import json
# 翻译函数
def fanyi(msg):
#参数封装
data = {
"kw": content
}
#参数拼接以及转码
data = parse.urlencode(data)
#请求地址
base_url = "http://fanyi.baidu.com/sug"
# Post
headers = {
"Content-Length": len(data), #动态计算d··ata长度
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
}
#封装一个request对象(地址,数据,headers)
req = request.Request(url=base_url, data=bytes(data, encoding="utf-8"), headers=headers)
#发起请求,并得到响应
response = request.urlopen(req)
#读取内容
html = response.read()
html = html.decode("utf-8") #转码
json_data = json.loads(html)#使用json格式化
#print(json_data)
# 整理数据
for item in json_data["data"]:
print(item["k"], item["v"])
if __name__ == "__main__":
#输入数据
content = input("请输入您要翻译的内容:")
#翻译
fanyi(content)
# https://tbmsg.baidu.com/gmessage/get?mtype=1&_=1521032999727
# https://tieba.baidu.com/f?kw=qq%E7%82%AB%E8%88%9E%E6%89%8B%E6%B8%B8&ie=utf-8&pn=50