将网站与文本链接

时间:2017-10-18 13:56:45

标签: python python-3.x

我想将文字与网站链接,但不知道问题所在。这是程序,这个网站检查文本中的感受:

import json
from urllib.request import urlopen
from urllib import parse



text= ("i hate you ")
i=parse.quote(text)
API_Call=urlopen( 'http://www.sentiment140.com/api/classify?text= '+ i )
f=json.loads(API_Call.read())
print (f)

1 个答案:

答案 0 :(得分:2)

我运行了你的代码并发现你遇到的问题是urlopen方法给你一个错误的请求异常。这是因为您在网址中有一个未转义的空格。删除空格,您将获得预期的输出。

API_Call=urlopen( 'http://www.sentiment140.com/api/classify?text= '+ i )更改为API_Call=urlopen( 'http://www.sentiment140.com/api/classify?text='+ i )text=之后删除的空格)以获得预期的输出。

PS:下次发布问题时,请为其他人提供完整的堆栈跟踪以找出问题。