Skip to main content
 首页 » 编程设计

python之在 Python 中使用正则表达式从苹果商店 html 中提取应用程序的描述

2025年05月04日31zhujiabin

我需要从 Apple store html 中提取应用程序的描述。说明介于两者之间

<p itemprop="description"> DESCRIPTION HERE </p> 

描述中包含一堆

符号、单词、空格等

显然 html 有很多其他文本和标签,因此需要非常精确的模式匹配。

谢谢

请您参考如下方法:

不要使用正则表达式来解析 HTML!

使用类似 BeautifulSoup! 的 HTML 解析器

>>> import bs4 
>>> s = '<p itemprop="description"> DESCRIPTION HERE </p>' 
>>> soup = bs4.BeautifulSoup(s, "html.parser") 
>>> soup.find("p", {"itemprop": "description"}).text 
>>> u' DESCRIPTION HERE ' 

或者如果你想找到所有元素:

>>> [item.text for item in soup.find_all("p", {"itemprop": "description"})] 
>>> [u' DESCRIPTION HERE ']