2012-05-14 3 views
1

test.txt에는 2 줄의 문장이 있습니다. 코드에서왜 HTML 코드를 표시 할 수 없습니까?

The heart was made to be broken. 
There is no surprise more magical than the surprise of being loved. 

: 나는 HTML 코드를 인쇄하려고

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 

. 텍스트 파일에 문장이 하나만있는 경우. 다 잘 작동하지만 텍스트 파일에 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

관련 문제