Regex 使用正则表达式解析html,有时它不会';行不通

Regex 使用正则表达式解析html,有时它不会';行不通,regex,Regex,我想用正则表达式解析以下文本。我想把td里面的所有东西都用“postcell”类。我正在使用这个代码,但它什么也没有给我 re.finditer('<td class="postcell">(.+?)</td>', doc) 如何获得所需的输出z z=[('1','a'),('b','2'),('c','3'),('d','4'),('e','5')] | 132K3125308 2012年3月24日16:40问 102 您忘记退出/ re.finditer(“(.+

我想用正则表达式解析以下文本。我想把td里面的所有东西都用“postcell”类。我正在使用这个代码,但它什么也没有给我

re.finditer('<td class="postcell">(.+?)</td>', doc)
如何获得所需的输出
z

z=[('1','a'),('b','2'),('c','3'),('d','4'),('e','5')]
| 132K3125308 2012年3月24日16:40问 102
您忘记退出
/

re.finditer(“(.+?)”,doc)


其他评论者是对的,一般来说,用正则表达式解析html是不可能的。就你的情况而言,这可能足够好了。要知道,正则表达式对嵌套是盲目的,因此,如果在一个post单元格中有一个
,那么您可能会遇到类似的边缘情况,您的匹配将提前结束。

请。这里至少说过一百万次:不要用正则表达式解析HTML或XML。使用HTMLDOM解析器。我不知道为什么在问另一个人为什么我不能用正则表达式解析HTML/XML之前,这里根本没有人做任何研究?找到数百万次提到为什么这不能做到。我怀疑这些人就是问为什么他们不能用锤子修理破碎的窗户的人。对这个答案最好的评论是:“我想是时候辞去助理的职务了,不要和Regex官员一起解析HTML。不管我们说多少次,他们每天都会来。。。甚至每小时。这是一个失败的事业,其他人可以争取一点。所以,如果必须的话,继续用正则表达式解析HTML。这只是破译的密码,不是生与死“来自博宾斯。所以让我们一起退出吧伙计们=/我记不清我说过多少次了:不要用regexThanks解析HTML或XML谢谢你的回答!我只是想知道为什么它会坏?是因为嵌套的标签吗?是的。如果您正在清除stackoverflow,那么由于标记或其他格式,如果帖子有
标记,您可能会遇到麻烦。我不确定是否有任何降价实际引入了该标记。例如,您的正则表达式将匹配
诸如此类
作为
<td class="postcell">
<div>
<div class="post-text" itemprop="text">
<p>Let us consider</p>
<pre class="lang-py prettyprint"><code>x = ['1', '2', '3', '4', '5']
y = ['a', 'b', 'c', 'd', 'e']
</code></pre>
<p>How do I get the required output <code>z</code>?</p>
<pre class="lang-py prettyprint"><code>z = [('1', 'a') , ('b', '2') , ('c', '3') , ('d', '4') , ('e', '5')]
</code></pre>
</div>
<div class="post-taglist">
<a href="/questions/tagged/python" class="post-tag js-gps-track" title="show questions tagged 'python'" rel="tag">python</a> <a href="/questions/tagged/list" class="post-tag js-gps-track" title="show questions tagged 'list'" rel="tag">list</a>
</div>
<table class="fw">
<tbody><tr>
<td class="vt">
<div class="post-menu"><a href="/q/9853438" title="short permalink to this question" class="short-link" id="link-post-9853438">share</a><span class="lsep">|</span><a href="/posts/9853438/edit" class="suggest-edit-post" title="">improve this question</a></div>
</td>
<td align="right" class="post-signature">
<div class="user-info user-hover">
<div class="user-action-time">
<a href="/posts/9853438/revisions" title="show all edits to this post">edited <span title="2012-03-24 16:42:59Z" class="relativetime">Mar 24 '12 at 16:42</span></a>
</div>
<div class="user-gravatar32">
<a href="/users/35070/phihag"><div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/6f92354195e8874dbee44d5c8714d506?s=32&amp;d=identicon&amp;r=PG" alt="" width="32" height="32" /></div></a>
</div>
<div class="user-details">
<a href="/users/35070/phihag">phihag</a>
<div class="-flair">
<span class="reputation-score" title="reputation score 132,147" dir="ltr">132k</span><span title="31 gold badges"><span class="badge1"></span><span class="badgecount">31</span></span><span title="252 silver badges"><span class="badge2"></span><span class="badgecount">252</span></span><span title="308 bronze badges"><span class="badge3"></span><span class="badgecount">308</span></span>
</div>
</div>
</div> </td>
<td class="post-signature owner">
<div class="user-info ">
<div class="user-action-time">
        asked <span title="2012-03-24 16:40:17Z" class="relativetime">Mar 24 '12 at 16:40</span>
</div>
<div class="user-gravatar32">
<a href="/users/1168528/karthik-reddi"><div class="gravatar-wrapper-32"><img src="https://www.gravatar.com/avatar/acce3b34402cd7646c175c273dee1616?s=32&amp;d=identicon&amp;r=PG" alt="" width="32" height="32" /></div></a>
</div>
<div class="user-details">
<a href="/users/1168528/karthik-reddi">Karthik Reddi</a>
<div class="-flair">
<span class="reputation-score" title="reputation score " dir="ltr">10</span><span title="2 bronze badges"><span class="badge3"></span><span class="badgecount">2</span></span>
</div>
</div>
</div>
</td>
</tr>
</tbody></table>
</div>
</td>