使用 Python 获取没有 class 属性的 div 标签的内容

2024-10-01 05:07:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图获取<div>标记中的文本,但无法获取。我正在尝试获取以下文本的内容:

MsiExec.exe /X{42435041-332D-5350-00A7-A758B70C0F00}

此标记不包含在任何div类中

<div style="margin-top: 10px;"><span class="colorlt">Uninstaller:</span> MsiExec.exe /X{42435041-332D-5350-00A7-A758B70C0F00}</div> 

有人能告诉我如何使用python吗? 我正在使用BeautifulSoup来删除页面


Tags: 标记margin文本div内容styletopexe
1条回答
网友
1楼 · 发布于 2024-10-01 05:07:07

这是这页的全部内容吗?如果是,请尝试以下方法:

from bs4 import BeautifulSoup
soup = BeautifulSoup('<div style="margin-top: 10px;"><span class="colorlt">Uninstaller:</span> MsiExec.exe /X{42435041-332D-5350-00A7-A758B70C0F00}</div>', 'html.parser')
print soup.div.text

Uninstaller: MsiExec.exe /X{42435041-332D-5350-00A7-A758B70C0F00}

如果刮取的页面包含其他div,这可能不起作用

相关问题 更多 >