【Python】百度翻译的爬虫实现(前篇)

该程序只能实现中文到英文的翻译 

import requests

import json

url = "http://fanyi.baidu.com/basetrans"

query_str = input("请输入要翻译成英文的内容:")

data={ "query": query_str,"from": "zh","to": "en"}


hearder={"User-Agent":
"Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
}

response = requests.post(url,data=data,headers=hearder)

html_str = response.content.decode()

#print(html_str)

#print(type(html_str))字符串类型

dict_ret = json.loads(html_str)

#print(dict_ret)

#print(type(dict_ret))字典类型

ret = dict_ret["trans"][0]["dst"]

print("翻译结果是:",ret)

 

你可能感兴趣的:(Python,爬虫)