2012-05-14 58 views
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>    
+2

http://docs.python.org/library/urllib.html#urllib .urlencode –

+0

'line_url'包含'\ n',嘗試打印出該變量。 – Levon

回答

0

也許換行的文件是一個問題就在這裏,請嘗試:

line_replace = line.replace(" ", "+").strip("\n") 
+0

謝謝。這行得通。 (必須等待幾分鐘才能接受答案) – ThanaDaray

相關問題