【发布时间】:2015-11-17 19:42:29
【问题描述】:
我需要正则表达式来从以下标签中提取文本: 我正在使用 Python 和 BeautifulSoup
<h4 style="color:#000000; line-height:20px; font-size:18px; margin-left:22px;
overflow:auto; content:inherit; padding:10px; font-family:"Book Antiqua",
Palatino, serif;">THE TEXT TO BE EXTRACTED IS HERE</h4></div><br /></div>
我尝试了以下方法:
stylecontent = 'color:#000000; line-height:20px; font-size:18px; margin-left:22px;
overflow:auto; content:inherit; padding:10px; font-family:"Book Antiqua",
Palatino, serif;'
soup = BeautifulSoup(br.response().read(), "lxml")
scrap_soup = soup.findAll('h4', {'style': stylecontent})
但它并不总是有效,因为网站不断变化stylecontent。
现在我想使用正则表达式:
soup.find_all(re.compile("some_foo_regex")):
我对那个some_foo_regex很感兴趣。
谢谢。
【问题讨论】:
-
您使用什么工具/语言?顺便说一句,这个 HTML 不正确(看引号)
-
查看您使用的语言存在哪些 HTML 解析器。 HTML 和正则表达式不能很好地结合在一起。
-
我投票决定将此问题作为离题结束,因为它是一个正则表达式问题,并且 OP 没有精确任何工具、语言或正则表达式风格。
-
请出示您的beautifulsoup相关代码。
标签: python regex web-scraping beautifulsoup