Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不能显示HTML代码?_Python - Fatal编程技术网

Python 为什么不能显示HTML代码?

Python 为什么不能显示HTML代码?,python,Python,在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 li

在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代码。如果文本文件中只有一句话。一切正常,但有一个错误,当文本文件中有两个句子(我想显示这两个句子的html代码)。有任何可能的解决方法吗

错误:

URLError: <urlopen error no host given>             
URLError:

可能文件中的换行符在此处有问题,请尝试:

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

行\u url
包含一个
\n
,请尝试打印该变量。谢谢。它起作用了。(必须等待几分钟才能接受答案)