【发布时间】:2017-08-27 05:31:05
【问题描述】:
我试图从 html 代码中提取文本。这是我的代码:
import re
Luna = open('D:\Python\Luna.txt','r+')
text=Luna.read()
txt=re.findall('<p>\s+(.*)</p>',text)
print txt
但是,它只消除了第一个 <p> 之前的部分,而第一个 <p> 之后的所有内容都保留了下来。我应该怎么做才能改进我的代码,使其只返回<p>和</p>之间的部分?
这是原始html代码的一部分:
src="/advjs/gg728x90.js"></script></td> </tr></table><div class="text" align="justify"></p><p> Sure. Eye of newt. Tongue of snake.</p><p> She added, “Since you’re taking Skills for Living, it’ll be good practice.”</p><p> For what? I wondered. Poisoning my family? “I have to baby-sit,” I said, a little too gleefully.</p>
【问题讨论】:
-
关于使用正则表达式解析 HTML 的强制性警告:RegEx match open tags except XHTML self-contained tags