在使用BeautifulSoup从页面抓取html后,我是新手,无法删除span标签 . 尝试使用“del links ['span']但它返回相同的结果 . 使用getText()的一些尝试失败了 . 显然我做错了应该很容易 . 帮助?
from bs4 import BeautifulSoup
import urllib.request
import re
url = urllib.request.urlopen("http://www.python.org")
content = url.read()
soup = BeautifulSoup(content)
for links in soup.find_all("span", text=re.compile(".com")):
del links['class']
print(links.)
2 回答
使用.unwrap() method删除标签,保留其内容:
根据您要执行的操作,您可以使用unwrap删除标记(实际上,使用其内容替换元素)或decompose以删除元素及其内容 .