Я хочу получить данные JSON из веб-интерфейса MyMemory (используя python).Тот же URL-запрос не работает с python-> urllib, но не с curl
Вот простой запрос для этого API:
http://api.mymemory.translated.net/get?langpair=en|fr&q=something+to+translate
Когда я пытаюсь получить данные с моим браузером или с завитком, он хорошо работает, давая мне обратно объект JSON, как это:
{"responseData":{"translatedText":"quelque chose \u00e0 traduire","match":0.85},"responseDetails":"","responseStatus":200,"responderId":"239","matches":[{"id":0,"segment":"something to translate","translation":"quelque chose \u00e0 traduire","quality":70,"reference":"Machine Translation provided by Google, Microsoft, Worldlingo or MyMemory customized engine.","usage-count":1,"subject":false,"created-by":"MT!","last-updated-by":"MT!","create-date":"2014-12-31 02:47:09","last-update-date":"2014-12-31 02:47:09","tm_properties":"","match":0.85},{"id":"443388028","segment":"to translate","translation":"traduire","quality":"68","reference":" |@| ","usage-count":1,"subject":" ","created-by":"IATE","last-updated-by":"IATE","create-date":"2014-11-04 01:54:57","last-update-date":"2014-11-04 01:54:57","tm_properties":null,"match":0.74},{"id":"476882062","segment":"To translate:","translation":"A traduire","quality":"74","reference":"","usage-count":1,"subject":"All","created-by":"Matecat","last-updated-by":"Matecat","create-date":"2014-12-04 11:04:23","last-update-date":"2014-12-04 11:04:23","tm_properties":"","match":0.71}]}
Но с питоном, используя URLLIB и точно такой же URL, сайт только дает мне этот выход:
can't open file
я написал небольшой пример, демонстрирующий питона мою проблему:
#!/usr/bin/python3
# coding: utf-8
import urllib.request
# The "Get" function of MyMemory API needs 2 mandatory parameters in the URL :
# - the text to translate (named 'q'),
# - and the two languages used to perform the translation,
# combined together with a pipe sign (|),
# for example : es|en (named 'langpair').
# An example list of URLs:
URLs = {
'MyMemory search, with mandatory "langpair" attribute' :
"http://api.mymemory.translated.net/get?" +
urllib.parse.urlencode({
'q' : 'something to translate',
'langpair' : 'en|fr',
}),
# http://api.mymemory.translated.net/get?langpair=en%7Cfr&q=something+to+translate
# ==> Response data: "can't open file"
# Didn't work : no JSON data at all, only this error message
'MyMemory subjects' : "http://api.mymemory.translated.net/subjects",
# ==> Response data: '["Accounting","Aerospace","Agriculture_and_Farming","Archeol'
# Ok, it worked
'Wikipedia' : "http://www.wikipedia.org",
# http://www.wikipedia.org
# ==> Response data: '<!DOCTYPE html>\n<html lang="mul" dir="ltr">\n<head>\n<!-- Syso' ...
# Ok, it worked
}
if __name__ == "__main__":
# For each URL in the list above:
for title, url in URLs.items():
# Display info:
print("Getting {} :".format(title))
print(' URL : ' + url)
# Open the URL:
data = urllib.request.urlopen(url)
# Print the beginning of the data received:
print(' Response data : {}\n'.format(data.read(60)))
Вот результат:
Getting MyMemory search, with mandatory "langpair" attribute :
URL : http://api.mymemory.translated.net/get?q=something+to+translate&langpair=en%7Cfr
Response data : b"can't open file"
Getting Wikipedia :
URL : http://www.wikipedia.org
Response data : b'<!DOCTYPE html>\n<html lang="mul" dir="ltr">\n<head>\n<!-- Syso'
Getting MyMemory subjects :
URL : http://api.mymemory.translated.net/subjects
Response data : b'["Accounting","Aerospace","Agriculture_and_Farming","Archeol'
Где это пойти не так? Кажется, ему не нравится пара «langpair» (из-за знака трубы, может быть?), Но я не понимаю!
Любая идея?
Использование 'import request data = request.get (url)' и печать 'data.content' works –
Спасибо, да, это работает очень хорошо! Мне все еще интересно, почему мой пример не работает, но это решение идеально. – yolenoyer
Не уверенный быть честным, но я всегда использую запросы, которые облегчают жизнь легче, чем нет. –