模式匹配未按预期工作

2024-03-29 10:19:18 发布

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

我在网站的不同html代码中玩模式匹配时,发现了一些奇怪的东西。我用了这个模式:

pat = <div class="id-app-orig-desc">.*</div>

我在play store的应用程序页面上使用了它(随机选择了一个应用程序)。所以根据我的说法,它应该只给出div标签之间的内容(即描述),但这不会发生。我给出了所有的东西,从第一个模式开始,一直到最后一页,完全忽略了中间的部分。有人知道发生了什么吗?!你知道吗

我检查了返回的列表的长度只有1。你知道吗


Tags: store代码dividapp应用程序play网站
1条回答
网友
1楼 · 发布于 2024-03-29 10:19:18

首先,do not parse HTML with regex,使用一个专门的工具-HTML解析器。例如,^{}

from bs4 import BeautifulSoup

data = """
<div>
    <div class="id-app-orig-desc">
        Do not try to get me with a regex, please.
    </div>
</div>
"""

soup = BeautifulSoup(data)
print soup.find('div', {'class': 'id-app-orig-desc'}).text.strip()

印刷品:

Do not try to get me with a regex, please.

相关问题 更多 >