BeautifulSoup 不会删除第 i 个元素

BeautifulSoup won't remove i element

我正在学习如何使用 beautiful soup 解析和操作 html,如下所示:

from lxml.html import parse
import urllib2
from urllib2 import urlopen
from BeautifulSoup import BeautifulSoup

url = 'some-url-here'
req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"}) 
parsed = urllib2.urlopen( req )
soup = BeautifulSoup(parsed)

for elem in soup.findAll(['script', 'style', 'i']):
    elem.extract()

for main_body in soup.findAll("div", {"role" : "main"}):
    print main_body.getText(separator=u' ')

结果包含 <i> 个标签,我不知道如何删除它们。这是如何实现的,为什么上面的代码没有删除唯一的标签?

问题实际上是您使用的是已弃用的 Beautifulsoup3,安装 bs4 一切正常:

In [10]: import urllib2
In [11]: from bs4 import BeautifulSoup # bs4

In [12]: url = 'https://www.gwr.com/'

In [13]: req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"})

In [14]: parsed = urllib2.urlopen(req)

In [15]: soup = BeautifulSoup(parsed,"html.parser")

In [16]: tags = soup.find_all(['script','style','i'])

In [17]: print(len(tags))
25

In [18]: for elem in tags:
   ....:         elem.extract()
   ....:     

In [19]: assert len(soup.find_all(['script','style','i'])) == 0

In [20]: