论一只爬虫的自我修养(第二天)
论一只爬虫的自我修养(第二天)
针对于有道翻译和百度翻译的区别:从form_data 中 对于有道翻译,在他的form_data里添加了salt与sign 来确定翻译结果的唯一性。那么百度翻译却不会出现这个问题
#现在开始编写
#coding=utf-8
import urllib
import json
#输入你想要翻译的内容
content = raw_input('请输入你要翻译的内容:')
#在百度翻译中翻译的地址url
url = “”
#在data_from中出现的参数归类
data = {}
data['from'] = 'zh'
data['to'] = 'en'
data['query'] = content
data['transtype'] = 'realtime'
data['simple_means_flag'] = 3
#确定data的编码格式
data = urllib.urlencode(data).encode('utf-8')
#返回一个response对象
response = urllib.urlopen(url,data)
#读取response的内容并且规定读取内容的编码格式
html = response.read().decode('utf-8')
target = json.loads(html)
#最后根据一层层的解剖,来打印最终想要的结果
print target['trans_result']['data'][0]['dst']

浙公网安备 33010602011771号