1
在test.txt中,我有兩行句子。爲什麼不能顯示HTML代碼?
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中只有一個句子。一切工作正常,但有一個錯誤,當有文本文件中的2個句子(我想顯示這兩個句子的HTML代碼。)任何可能的方式來解決這個問題?
錯誤:
URLError: <urlopen error no host given>
http://docs.python.org/library/urllib.html#urllib .urlencode –
'line_url'包含'\ n',嘗試打印出該變量。 – Levon