Python Requests URL with Unicode Parameters

后端 未结 3 2120
闹比i
闹比i 2021-01-06 07:25

I\'m currently trying to hit the google tts url, http://translate.google.com/translate_tts with japanese characters and phrases in python using the requests library.

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-06 08:01

    The user agent can be part of the problem, however, it is not in this case. The translate_tts service rejects (with HTTP 403) some user agents, e.g. any that begin with Python, curl, wget, and possibly others. That is why you are seeing a HTTP 403 response when using urllib2.urlopen() - it sets the user agent to Python-urllib/2.7 (the version might vary).

    You found that setting the user agent to Mozilla/5.0 fixed the problem, but that might work because the API might assume a particular encoding based on the user agent.

    What you actually should do is to explicitly specify the URL character encoding with the ie field. Your URL request should look like this:

    http://translate.google.com/translate_tts?ie=UTF-8&tl=ja&q=%E3%81%B2%E3%81%A8%E3%81%A4
    

    Note the ie=UTF-8 which explicitly sets the URL character encoding. The spec does state that UTF-8 is the default, but doesn't seem entirely true, so you should always set ie in your requests.

    The API supports kanji, hiragana, and katakana (possibly others?). These URLs all produce "nihongo", although the audio produced for hiragana input has a slightly different inflection to the others.

    import requests
    
    one = u'\u3072\u3068\u3064'
    kanji = u'\u65e5\u672c\u8a9e'
    hiragana = u'\u306b\u307b\u3093\u3054'
    katakana = u'\u30cb\u30db\u30f3\u30b4'
    url = 'http://translate.google.com/translate_tts'
    
    for text in one, kanji, hiragana, katakana:
        r = requests.get(url, params={'ie': 'UTF-8', 'tl': 'ja', 'q': text})
        print u"{} -> {}".format(text, r.url)
        open(u'/tmp/{}.mp3'.format(text), 'wb').write(r.content)
    

提交回复
热议问题