我试图像这样将一些文本传递给readability API

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


我在最后一行收到此错误:


  [Errno 2]没有这样的文件或目录:'http://ipeirotis.appspot.com/readability/GetReadabilityScores?format=json&text=this+reminds+me+of+the+Dutch+2001a+caravan+full+of+smoky + people + Auld + Lang + Syne'


但是,错误中的URL是有效的,并在您访问它时返回响应。如何编码URL,以便可以使用urlopen?非常感谢。

最佳答案

您引用的是完整的URL,包括http://,但没有。如果您尝试打印request_url的实际值,则会得到

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


这不是您想要的。您只想引用要成为网站单个参数的部分。我尝试了以下方法,它似乎有效:

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

09-10 06:34