Python如何在HTML中找到具有模式的所有子字符串?

2024-10-03 09:20:37 发布

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

我正在使用Python读取HTML数据,但我很难从该HTML中找到“d:Title>;从优秀到卓越<;/d:Title>;”之间的所有子字符串

data = "<html><head></head><body><pre style='word-wrap': break-word; white-space: pre-wrap;
d:Title&gt;Good To Great&lt;/d:Title&gt;d:ComplianceAssetId m:null='true'/&gt;
d:Title&gt;War and Peace&lt;/d:Title&gt;/d:ComplianceAssetId m:null='false'/&gt; 
d:Title&gt;The Great Gatsby&lt;/d:Title&gt;/entry&gt;&lt;/feed&gt;</pre></body></html>"

预期产出:

['Good To Great', 'War and Peace', 'The Great Gatsby']

我怀疑regex可能是一个解决方案,但我对regex的了解有限(仍在学习),有人能帮我解决这个问题吗

提前感谢你的帮助


Tags: toltgttitlehtmlbodyprenull
2条回答
>>> re.findall('Title&gt;(.*)&lt;/d:Title', data)
['Good To Great', 'War and Peace', 'The Great Gatsby']

可以使用通配符.查找文本

正则表达式是'Title&gt;([\w\s]+)&lt;/d:Title'

solution output

Python版本3.7。我希望这有帮助

相关问题 更多 >