Question

Je suis en train de passer un texte à cette API lisibilité comme ceci:

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))

je reçois cette erreur sur la dernière ligne si:

[Errno 2] Aucun fichier ou répertoire: « http://ipeirotis.appspot.com/readability/GetReadabilityScores?format=json&text=this+reminds+me+of+the+Dutch+2001a+caravan+full+ + de fumée + personnes + Auld Lang + + Syne '

Cependant, l'URL dans l'erreur est valide et renvoie une réponse lorsque vous visitez. Comment puis-je encoder l'URL afin que je puisse utiliser urlopen? Merci beaucoup.

Était-ce utile?

La solution

You are quoting the full url, including the http:// and what not. If you try to print the actually value of request_url, you get

>>> 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

Which is not what you want. You only want to quote the parts that you want to be a single argument to the website. I tried the following and it seemed to work:

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))

Autres conseils

Use urllib.urlencode to encode only the query string, like so:

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

Encoding the entire URL will encode the slashes and colons, and you want those to remain unencoded so it will be parsed properly as a URL (and not mistaken for a local file).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top