http协议-数据提交

编写程序实现通过有道或百度翻译url对用户输入数据进行翻译: 进入有道翻译或者百度翻译页面,找出页面向服务器提交待翻译数据使用的url; 使用浏览器分析工具分析相关需要提交的数据字段以及值,主要包括:(1)待翻译词语或句子使用的字段数据;(2)待翻译数据使用的语言;(3)翻译结果使用的语言;(4)返回的翻译结果使用的数据类型;(5)其他需要的数据字段 获取使用requests库post方法提交数据所需其他数据,如headers中使用的数据信息(如User-Agent,cookies等) 实现完整的程序,并运行程序,对输入的待翻译的内容的返回结果进行验证;如果出现返回为空值等问题,请对可能的原因进行分析,并给出可行的解决方案。

import requests
import json
while True:
    content = input("请输入要翻译的词语:")
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    post_form = {
        'i': content,
        'from': 'AUTO',
        'to': 'AUTO',
        'smartresult': 'dict',
        'client': 'fanyideskweb',
        'salt': '15584130884799',
        'sign': '7cb8e5dada47bfefe2cfee6c2e586375',
        'ts': '1558413088479',
        'bv': '66745c2bd404c2f62490e4e8dadb4b0e',
        'doctype': 'json',
        'version': '2.1',
        'keyfrom': 'fanyi.web',
        'action': 'FY_BY_CLICKBUTTION'
    }
    response = requests.post(url,data=post_form)
    trans_json = response.text
    trans_dict = json.loads(trans_json)
    result = trans_dict['translateResult'][0][0]['tgt']
    print("翻译结果:"+result)
    print()

资源已经上传至资源库

发布了90 篇原创文章 · 获赞 37 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_25368751/article/details/103648140