Python 有道翻译爬虫|爬取json接口

发表时间:2019-12-19

完整脚本:

from urllib import request, parse
import json

name = '''Inside Chinese smartphone maker Oppo's Shenzhen factory, the company is twisting, dropping, and tapping its devices to make sure they're ready for primetime.'''
def youdaofanyi(name):
req_url = 'http://fanyi.youdao.com/translate' # 创建连接接口
# 创建要提交的数据
Form_Date = {}
Form_Date['i'] = name
Form_Date['doctype'] = 'json'

data = parse.urlencode(Form_Date).encode('utf-8') # 数据转换
response = request.urlopen(req_url, data) # 提交数据并解析
html = response.read().decode('utf-8') # 服务器返回结果读取
# print(html)
# 可以看出html是一个json格式
translate_results = json.loads(html) # json格式载入
translate_results = translate_results['translateResult'][0][0]['tgt'] # json格式调取
print(translate_results)
return (translate_results) # 输出结果

if __name__ == '__main__':
youdaofanyi(name)


运行后效果截图:

Snipaste_2019-12-19_21-21-40.png

文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890

微配音

Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free