以下是我目前掌握的情况:
from bs4 import BeautifulSoup
def cleanme(html):
soup = BeautifulSoup(html) # create a new bs4 object from the html data loaded
for script in soup(["script"]):
script.extract()
text = soup.get_text()
return text
testhtml = "<!DOCTYPE HTML>\n<head>\n<title>THIS IS AN EXAMPLE </title><style>.call {font-family:Arial;}</style><script>getit</script><body>I need this text captured<h1>And this</h1></body>"
cleaned = cleanme(testhtml)
print (cleaned)
正在删除脚本
你好像快拿到了。您还需要删除html标记和css样式代码。这是我的解决方案(我更新了函数):
您可以使用^{} 从文档中完全删除标记,并使用^{} 生成器检索标记内容。
以干净的方式删除指定的标记和注释。感谢Kim Hyesung对this code的支持。
相关问题 更多 >
编程相关推荐