在test.txt中,我有两行句子。
The heart was made to be broken.
There is no surprise more magical than the surprise of being loved.
代码:
import urllib2
file = open('/Users/name/Desktop/textfile.txt','r')
data = file.readlines()
file.close()
countline = 0
for line in data:
countline = countline + 1
line_replace = line.replace(" ", "+")
line_url = 'http://sentistrength.wlv.ac.uk/results.php?text=' + line_replace + '&submit=Detect+Sentiment'
requesturl = urllib2.Request(line_url)
openurl = urllib2.urlopen(requesturl)
readurl = openurl.read()
print readurl
我正在尝试打印出HTML代码。如果Textfile中只有一个句子。一切正常但文本文件中有两个句子时出错(我想显示两个句子的html代码。)任何可能的方法来解决这个问题?
错误:
URLError: <urlopen error no host given>
答案 0 :(得分:0)
可能文件中的换行是一个问题,请尝试:
line_replace = line.replace(" ", "+").strip("\n")