Python urlopen/read返回带有html标记的文本,而不是纯文本
我只是想复制在-上给出的代码,以便从web读取数据。。 本章中提到了这一点Python urlopen/read返回带有html标记的文本,而不是纯文本,python,html,python-3.x,urlopen,Python,Html,Python 3.x,Urlopen,我只是想复制在-上给出的代码,以便从web读取数据。。 本章中提到了这一点 >>> from urllib import request >>> url = "http://www.gutenberg.org/files/2554/2554.txt" >>> response = request.urlopen(url) >>> raw = response.read().decode('utf8') >>>
>>> from urllib import request
>>> url = "http://www.gutenberg.org/files/2554/2554.txt"
>>> response = request.urlopen(url)
>>> raw = response.read().decode('utf8')
>>> type(raw)
<class 'str'>
>>> len(raw)
1176893
>>> raw[:75]
'The Project Gutenberg EBook of Crime and Punishment, by Fyodor Dostoevsky\r\n'
这将返回没有html标记的文本。但是我得到了带有标签的输出 请参见下面的
令牌输出:
[、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
我应该怎么做才能将纯文本作为输出?我认为更改为您的url是绝对正确的。我想知道您是如何找到这个正确的url的。我做了一些尝试。我了解到,通过访问“”可以看到正确的文本文件名和html目录。非常感谢。
from urllib import request
#url = "http://www.gutenberg.org/files/2554/2554.txt"
#(original as per the chapter sample code.. but gives error hence
#changed to below url string.)
url = "http://www.gutenberg.org/files/2554/2554-h/2554-h.htm"
response = request.urlopen(url)
raw = response.read().decode('utf8')
print('data type of raw = ', type(raw))
print('length of raw = ', len(raw))
print('initial contents - ', raw[:175])
tokens = word_tokenize(raw)
print('tokens\n', tokens[:100])