欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

requests+urlencode+UA在线菲英互翻

程序员文章站 2022-07-08 15:50:11
import requestsimport jsonfrom urllib.parse import urlencodedef get_trans(sl='tl',tl='en',q='Pwede mo bang bagalan ang iyong pagsasalita?'): heads = {} heads['User-Agent'] = 'Mozilla/5.0 ' \ '(Macintosh; U; Intel Mac...
import requests
import json
from urllib.parse import urlencode



def get_trans(sl='tl',tl='en',q='Pwede mo bang bagalan ang iyong pagsasalita?'):
    heads = {}
    heads['User-Agent'] = 'Mozilla/5.0 ' \
                          '(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ' \
                          '(KHTML, like Gecko) Version/5.1 Safari/534.50'
    params = {
      "sl": sl,
      "tl": tl
    }
    query = urlencode(params)
    url = 'https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&' + query + '&q=' +q.replace(' ','%20')
    #print(url)
    res = requests.get(url, headers=heads)
    res.encoding = 'utf-8'
    meaning = json.loads(res.text,encoding='utf-8')[0][0][0]
    print(q + '    ***After translating***    ' +meaning)
    return meaning
get_trans('en','tl', q='Can you slow down your speech?')
get_trans(q = "Magandang araw sa'yo!")

本文地址:https://blog.csdn.net/qq_31101891/article/details/110915341

相关标签: 在线翻译 爬虫