bs4去除html标签,python - 如何使用BeautifulSoup从html清除标签 - 堆栈内存溢出

我正在尝试使用NLTK库训练数据。 我按照一步一步的过程。 我做了第一步,但在做第二步时,我收到以下错误:

TypeError: a bytes-like object is required, not 'list'

我尽力纠正它,但我再次遇到同样的错误。

这是我的代码:

from bs4 import BeautifulSoup

import urllib.request

response = urllib.request.urlopen('http://php.net/')

html = response.read()

soup = BeautifulSoup(html,"html5lib")

text = soup.get_text(strip=True)

print (text)

这是我的错误

C:\python\lib\site-packages\bs4\__init__.py:181: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 8 of the file E:/secure secure/chatbot-master/nltk.py. To get rid of this warning, change code that looks like this:

BeautifulSoup(YOUR_MARKUP})

to this:

BeautifulSoup(YOUR_MARKUP, "html5lib")

markup_type=markup_type))

Traceback (most recent call last):

File "E:/secure secure/chatbot-master/nltk.py", line 8, in

soup = BeautifulSoup(html)

File "C:\python\lib\site-packages\bs4\__init__.py", line 228, in __init__

self._feed()

File "C:\python\lib\site-packages\bs4\__init__.py", line 289, in _feed

self.builder.feed(self.markup)

File "C:\python\lib\site-packages\bs4\builder\_html5lib.py", line 72, in feed

doc = parser.parse(markup, **extra_kwargs)

File "C:\python\lib\site-packages\html5lib\html5parser.py", line 236, in parse

parseMeta=parseMeta, useChardet=useChardet)

File "C:\python\lib\site-packages\html5lib\html5parser.py", line 89, in _parse

parser=self, **kwargs)

File "C:\python\lib\site-packages\html5lib\tokenizer.py", line 40, in __init__

self.stream = HTMLInputStream(stream, encoding, parseMeta, useChardet)

File "C:\python\lib\site-packages\html5lib\inputstream.py", line 148, in HTMLInputStream

return HTMLBinaryInputStream(source, encoding, parseMeta, chardet)

File "C:\python\lib\site-packages\html5lib\inputstream.py", line 416, in __init__

self.rawStream = self.openStream(source)

File "C:\python\lib\site-packages\html5lib\inputstream.py", line 453, in openStream

stream = BytesIO(source)

TypeError: a bytes-like object is required, not 'list'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值