简体   繁体   English

JSONDecoder 错误 - 使用 Python 访问 Google 翻译时 API

[英]JSONDecoder Error - While accessing Google Translate API with Python

I am learning to use HTTP requests in Python, using this HTTP request provided by a TopCoder training challenge (learning purposes only: no compensation of any sort) in which you have to access the Google Translate API:我正在学习在 Python 中使用 HTTP 请求,使用由 TopCoder 培训挑战提供的 HTTP 请求(仅限学习目的:在您可以访问的 Google 翻译 API 中没有任何补偿:)

curl --location --request POST 'https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=%25s&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e&' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'User-Agent: AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1' \
--data-urlencode 'sl=de' \
--data-urlencode 'tl=en' \
--data-urlencode 'q=Hallo' 

and I'm wondering how to make the equivalent request in my Python application?我想知道如何在我的 Python 应用程序中提出等效请求? Any help is appreciated.任何帮助表示赞赏。

So far I have:到目前为止,我有:

  • installed and imported requests安装和导入的requests
  • understood that I need to store my POST request in a variable and parse it with JSON.了解到我需要将我的 POST 请求存储在一个变量中并使用 JSON 对其进行解析。

The issue is that I get a JSONDecoder error问题是我收到 JSONDecoder 错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\myname\Documents\Code\GoogleTranslateApiPy\env\lib\site-packages\requests\models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "c:\users\myname\appdata\local\programs\python\python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "c:\users\myname\appdata\local\programs\python\python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\users\myname\appdata\local\programs\python\python38-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

with this Python request (I tried to translate the curl request as best as I could):有了这个 Python 请求(我试图尽可能地翻译 curl 请求):

import requests

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1',
}

params = (
    ('client', 'at'),
    ('dt', ['t', 'ld', 'qca', 'rm', 'bd']),
    ('dj', '1'),
    ('hl', '%s'),
    ('ie', 'UTF-8'),
    ('oe', 'UTF-8'),
    ('inputm', '2'),
    ('otf', '2'),
    ('iid', '1dd3b944-fa62-4b55-b330-74909a99969e'),
    ('', ''),
)

response = requests.get('https://translate.google.com/translate_a/single', headers=headers, params=params)

I feel that there's something fundamental I'm missing here.我觉得这里缺少一些基本的东西。 The request in the current documentation by Google for Translate differs from this provided request, but I'd like to know how I could get this way to work, in case I'm ever provided with a curl command like this in the future.谷歌当前文档中的翻译请求与此提供的请求不同,但我想知道我如何才能以这种方式工作,以防我将来收到这样的 curl 命令。

Two things:两件事情:

  1. You must do a POST request (currently you are doing a get request)您必须执行POST 请求(当前您正在执行获取请求)
  1. You are not including the body of the request.您不包括请求的正文。 For example, the curl call includes url encoded data like this: data-urlencode 'q=Hallo'.例如,curl 调用包括 url 编码数据,如下所示:data-urlencode 'q=Hallo'。 You must include this parameters in your post request too, the provided link shows you how.您也必须在您的发布请求中包含此参数,提供的链接向您展示了如何。 This are key values that will go inside a dictionary, for example {q: 'Hallo', ...}这是字典中的 go 的键值,例如 {q: 'Hallo', ...}

PS: I'm 90% sure that you should also convert to a dictionary the query params you currently have inside tuples. PS:我 90% 确定您还应该将当前在元组中的查询参数转换为字典。 So, you'd have post with headers, params and data.因此,您将发布带有标题、参数和数据的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM