Requests抓取有道翻译结果
Requests比urllib更加方便,抓取有道翻译非常的简单。
import requests
class YouDao():
def __init__(self,parm):
# 请求表单,不需要手动urlencode
self.form_data = {}
self.form_data['type'] = 'AUTO'
self.form_data['i'] = parm
self.form_data['doctype'] = 'json'
self.form_data['keyfrom'] = 'fanyi.web'
self.form_data['ue'] = 'UTF-8'
self.form_data['action'] = 'FY_BY_CLICKBUTTON'
self.form_data['typoResult'] = 'true'
def fanyi(self):
r = requests.get('http://fanyi.youdao.com/translate',params = self.form_data)
# 获得json数据
target = r.json()
target = target['translateResult'][0][0]
print("%s --> %s"%(target['src'],target['tgt']))
if __name__ == '__main__':
youdao = YouDao('很傻很天真')
youdao.fanyi()
运行结果:
######## 华 丽 的 分 割 线 ########
附上一个好用的json视图查看器: