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()

  

posted @ 2017-08-23 14:16  RoyFans  阅读(3886)  评论(4编辑  收藏  举报