从html脚本中删除所有'b'标签及其内容

时间:2020-11-06 03:09:14

标签: python beautifulsoup

当我想尝试摆脱所有b标签及其内容并仅保留其余文本时,我正在使用漂亮的汤,并使用Shrek脚本来适应它。

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

url = "https://www.imsdb.com/scripts/Shrek.html"
ua = UserAgent(verify_ssl=False)
headers = {'User-Agent': 'ua.chrome'}


def get_script():
    script_text = requests.get(url, headers=headers)

    soup = BeautifulSoup(script_text.text, 'html.parser')
    script = soup.find('td', class_='scrtext')
    tag = script.find_all('b')
    if tag is None:
        pass
    else:
        tag.clear()

    print(tag)

get_script()

这是我使用的代码

如果要注释掉if和else语句并运行代码,则id显示所有b标记及其内容,如果要取消注释并运行它,它将返回[]。因此,标签已被删除和删除。问题是,当我使用print(script)而不是print(tag)时,它只是正常返回脚本,而不删除任何b标记或它们的内容,即使它们已被删除。

有人知道为什么吗?

2 个答案:

答案 0 :(得分:1)

要删除ReactDOM.render(<Input placeholder="Basic usage" />, document.getElementById('container')); 标签,您应该像这样使用from PyQt5.QtWidgets import * from PyQt5.QtCore import * import ctypes.wintypes import logging from win32con import * import win32api class W(QTabWidget): def nativeEvent(self, eventType, message): click_menu = QMenu(self) click_menu.addAction("Yay") try: msg = ctypes.wintypes.MSG.from_address(message.__int__()) except: logging.error("", exc_info=True) if eventType == "windows_generic_MSG": if msg.message == WM_NCLBUTTONDOWN: mouse_x = win32api.LOWORD(msg.lParam) mouse_y = win32api.HIWORD(msg.lParam) frame = self.frameGeometry() content = self.geometry() print(mouse_x, mouse_y, frame, content) if mouse_y < content.y() and mouse_y >= frame.y(): click_menu.exec_(QPoint(mouse_x, mouse_y)) return False, 0 app = QApplication([]) w = W() w.resize(1000,100) w.move(0,0) w.show() app.exec_()

<b>

答案 1 :(得分:0)

您需要做的就是Nvm

 for b in tag:
        b.clear()
        b.decompose()
 

这将删除所有b标记及其内容,同时保持脚本的其余部分不变,这就是我想要的。

相关问题