我有以下的Python程序:如何在python中獲得html2text的清晰輸出?
import urllib.request as urllib2
import html2text
html = urllib2.urlopen("http://www.stern.de/")
page_source = html.read()
h = html2text.HTML2Text()
h.ignore_links = True
h.ignore_images = True
text = h.handle(str(page_source))
print (text)
輸出是:
\n \n\n
* \n Anmelden
\n\n
* \n
Sie haben noch keinen Account?
\n Kostenlos neu registrieren
\n \n
\n
我怎樣可以過濾掉 「\ n」?
我試了一下,例如這種方式,它不工作:
wordList = text.split()
for word in wordList:
if word != "\n":
print (word)
這是分裂後的輸出:
\n\n
*
\n
Anmelden
\n\n
*
\n
Sie
haben
noch
keinen
Account?
\n
Kostenlos
neu
registrieren
\n
\n
\n
所以我的檢查沒有工作。如何檢查\ n換行符號?
這就是換行符。如果您打印它,它將「消失」(相反,正確地打破線條而不是顯示爲'\ n')。你真的想把所有的文本都放在一起嗎? –
我希望將每個單詞分隔爲一個數組。如果我不忽略它\ n被識別爲一個單詞 –
'text.split()'將把空格作爲空格 – jonrsharpe