我正在寻找一种方法来删除 ADO 维基页面上“内容”标签中保存的记录。
我已经检查了我的 url 页面所包含的 json 内容:
def wiki_update(vm_list):
pyautogui.hotkey('f5')
pat = get_or_set_secret(kv_url, pat_secret_name, " ")
authorization = base64.b64encode(f":{pat}".encode('ascii')).decode('ascii')
basic_auth_value = f"Basic {authorization}"
get_headers = {"Authorization": basic_auth_value}
get_response = requests.get(wiki_url, verify=True, headers=get_headers)
content = json.loads(get_response.text)
print(content.content)
输出为:
{'path': '/MyWikiPage', 'order': 1, 'gitItemPath': '/Test%2DDebugVM%2DRemove%2DContent.md', 'subPages': [], 'url': 'https://dev.azure.com/xxx/123456789a6ba0154ffce/_apis/wiki/wikis/12345981-0649bdzsbf7aad/pages/%2FTest-DebugVM-Remove-Content', 'remoteUrl': 'https://dev.azure.com/xxx/123456789a6ba0154ffce/_apis/wiki/wikis/12345981-0649bdzsbf7aad?pagePath=%2FMyWikiPage', 'id': 14523, 'content': '\n|virtual machine name | ADO ticket | owner Email | creation date |\n|--|--|--|--|\n| my-12345-vm-0 | #12345 | [email protected] | 29-01-2024 |'}
我要删除里面的项目的具体标签是“内容”:
'content': '\n|virtual machine name | ADO ticket | owner Email | creation date |\n|--|--|--|--|\n| my-12345-vm-0 | #12345 | [email protected] | 29-01-2024 |'
我确实想保留标题,所以我需要帮助删除的项目是:
my-12345-vm-0 | #12345 | [email protected] | 29-01-2024 |
假设 HTML 文档结构示例如下:
html_content = """
<html>
<body>
<p>Hello World.</p>
<div id="to_be_removed">I will not exist.</div>
</body>
</html>
"""
我们假设需要从HTML结构中删除ID为
to_be_removed
的元素,我们需要使用extract
方法:
soup = BeautifulSoup(html_content, 'html.parser')
to_be_removed = soup.find('div', {'id': 'to_be_removed'})
if to_be_removed:
to_be_removed.extract()
最后,我们可以将 soup 转换为字符串并获取更新后的 HTML:
modified_html = str(soup)
print(modified_html)