Domanda

Sto cercando di passare un po 'di testo a questo API di leggibilità così:

text = 'this reminds me of the Dutch 2001a caravan full of smoky people Auld Lang Syne'
# construct Readability Metrics API url
request_url = 'http://ipeirotis.appspot.com/readability/GetReadabilityScores?format=json&text=%s' % text
request_url = urllib.quote_plus(request_url.encode('utf-8'))
# make request
j = json.load(urllib.urlopen(request_url))

Ricevo questo errore sull'ultima riga:

ERRNO 2] Nessun file o directory di questo tipo: 'http://ipeirotis.appspot.com/redability/getreabilityscores?format=json&text=this+reminds+me+of+the+Dutch+2001a+Caravan+fullol+of+SmokyyOfykykykykykykykykykykykykykykykykykyy +persone+auld+lang+syne '

Tuttavia, l'URL nell'errore è valido e restituisce una risposta quando lo visiti. Come posso codificare l'URL in modo da poter usare Urlopen? Molte grazie.

È stato utile?

Soluzione

Stai citando l'intero URL, incluso http: // e cosa no. Se provi a stampare il valore effettivamente di request_url, ottieni

>>> print request_url
http%3A%2F%2Fipeirotis.appspot.com%2Freadability%2FGetReadabilityScores%3Fformat
%3Djson%26text%3Dthis+reminds+me+of+the+Dutch+2001a+caravan+full+of+smoky+people
+Auld+Lang+Syne

Non è quello che vuoi. Vuoi solo citare le parti che vuoi essere un singolo argomento sul sito Web. Ho provato quanto segue e sembrava funzionare:

text = 'this reminds me of the Dutch 2001a caravan full of smoky people Auld Lang Syne'
# construct Readability Metrics API url
request_url = 'http://ipeirotis.appspot.com/readability/GetReadabilityScores?format=json&text=%s' % urllib.quote_plus(text.encode('utf-8'))
# make request
j = json.load(urllib.urlopen(request_url))

Altri suggerimenti

Usa urllib.urlencode per codificare solo la stringa di query, come: così:

request_url = 'http://ipeirotis.appspot.com/readability/GetReadabilityScores?%s' % urllib.urlencode({'format': 'json', 'text': text})

La codifica dell'intero URL codificherà le barre e i due punti e vuoi che rimangano non codificati, quindi verrà analizzato correttamente come URL (e non scambiato per un file locale).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top