2014-12-31 80 views
1

我想從MyMemory Web API(使用python)檢索JSON數據。相同的URL請求失敗與python-> urllib但不捲曲

下面是該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而完全相同的網址,該網站只給了我這樣的輸出:

can't open file 

我寫了一個簡短的Python例子來展示我的問題:

#!/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'參數(因爲管道標誌,也許?),但我不明白!

任何想法?

+2

使用'導入請求數據= requests.get(url)'和打印'data.content'作品 –

+0

謝謝,是的,它工作的很好!我仍然想知道爲什麼我的例子不起作用,但這個解決方案是完美的。 – yolenoyer

+0

不確定是否誠實,但我總是使用請求,這會讓生活變得更容易。 –

回答

3

只是爲了解釋問題所在。您需要提供User-Agent頭:

request = urllib.request.Request(url) 
request.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36') 

data = urllib.request.urlopen(request) 

嘗試,它打印:

Getting MyMemory search, with mandatory "langpair" attribute : 
    URL : http://api.mymemory.translated.net/get?langpair=en%7Cfr&q=something+to+translate 
    Response data : b'{"responseData":{"translatedText":"quelque chose \\u00e0 trad' 

除此之外,是的,使用requests通常可以節省大量的時間和頭痛,這是for humans

+1

我只是想試試這個,救了我一些打字;) –

+0

它的工作原理,但我會嘗試請求庫,它看起來很容易。使用urllib你需要每次都放這個頭文件嗎? – yolenoyer

+0

@yolenoyer yup,每次。 – alecxe