1、Python request(爬虫-百度翻译)

#encoding=utf-8

import requests
def fanyi():
    while True:
        context = input("请输入翻译的内容(退出q):")

        if context in ['q','Q']:
            break
        else:
            url = 'http://fanyi.baidu.com/v2transapi/'
            data = {
                'from':'en',
                'to':'zh',
                'query':context,
                'transtype':'translang',
                'simple_means_flag':'3',
            }
            headers ={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0'}
            response = requests.post(url,data,headers=headers)
            head = response.headers

            # text = response.text
            # text = json.loads(text)
            # res = text['trans_result']['data'][0]['dst']
            # print(res)
            print(head['Content-Type'])
            print(response.json()['trans_result']['data'][0]['dst'])

fanyi()

  

原文地址:https://www.cnblogs.com/royfans/p/7417914.html