Regex 正则表达式,这个正则表达式怎么了?

Regex 正则表达式,这个正则表达式怎么了?,regex,Regex,首先,我为这个糟糕的题目感到抱歉,但我想不出更好的题目了 因此,我试图用Python构建一个小工具来提高我的技能,它从Imdb.com上刮取数据,并输出标题和从HTML中过滤的其他内容 我正在使用这个正则表达式进行搜索:Titles[\s]{0,3}.*?这应该会导致a>标题之后和之前的一切,但我做错了什么。我添加了[\s]{0,3},因为我认为这可能是因为\n或其他原因,但它根本没有修复它 这是源代码块: <div class="findSection"> <h3 class

首先,我为这个糟糕的题目感到抱歉,但我想不出更好的题目了

因此,我试图用Python构建一个小工具来提高我的技能,它从Imdb.com上刮取数据,并输出标题和从HTML中过滤的其他内容

我正在使用这个正则表达式进行搜索:Titles[\s]{0,3}.*?这应该会导致a>标题之后和之前的一切,但我做错了什么。我添加了[\s]{0,3},因为我认为这可能是因为\n或其他原因,但它根本没有修复它

这是源代码块:

<div class="findSection">
<h3 class="findSectionHeader"><a name="tt"></a>Titles</h3>
<table class="findList">
<tr class="findResult odd"> <td class="primary"> <a href="/title/tt1474684/?ref_=fn_al_tt_1" >
<img src="https://images-na.ssl-images-amazon.com/images/M/_AL_.jpg" />
</a> </td> <td class="result_text"> 
<a href="/title/tt1474684<a href="/title/tt3155298/?ref_=fn_al_tt_3" >
<img src="http://ia.media-imdb.com/imagestd class="primary_photo"> 
<a href="/tiopicture/32x44/film-3119741174._CB522736599_.png" /></a>
</td> <td class="result_text"> 
<a href="/title/tt1501661/?ref_=fn_al_tt_10" >Luther</a> (1968) (TV Movie) </td> </tr></table>
尝试使用以下正则表达式:

蟒蛇

您可以将标记re.DOTALL添加到您的re调用中,以便。匹配换行符:

src = '''<div class="findSection">
<h3 class="findSectionHeader"><a name="tt"></a>Titles</h3>
<table class="findList">
<tr class="findResult odd"> <td class="primary"> <a href="/title/tt1474684/?ref_=fn_al_tt_1" >
<img src="https://images-na.ssl-images-amazon.com/images/M/_AL_.jpg" />
</a> </td> <td class="result_text"> 
<a href="/title/tt1474684<a href="/title/tt3155298/?ref_=fn_al_tt_3" >
<img src="http://ia.media-imdb.com/imagestd class="primary_photo"> 
<a href="/tiopicture/32x44/film-3119741174._CB522736599_.png" /></a>
</td> <td class="result_text"> 
<a href="/title/tt1501661/?ref_=fn_al_tt_10" >Luther</a> (1968) (TV Movie) </td> </tr></table>'''

expr = r'<h3 class="findSectionHeader"><a name="tt"><\/a>Titles<\/h3>[\s]{0,3}(.*?)<\/td> <\/tr><\/table>'

import re

print re.findall( expr, src, re.DOTALL )
收益率:

['<table class="findList">\n<tr class="findResult odd"> <td class="primary"> <a href="/title/tt1474684/?ref_=fn_al_tt_1" >\n<img src="https://images-na.ssl-images-amazon.com/images/M/_AL_.jpg" />\n</a> </td> <td class="result_text"> \n<a href="/title/tt1474684<a href="/title/tt3155298/?ref_=fn_al_tt_3" >\n<img src="http://ia.media-imdb.com/imagestd class="primary_photo"> \n<a href="/tiopicture/32x44/film-3119741174._CB522736599_.png" /></a>\n</td> <td class="result_text"> \n<a href="/title/tt1501661/?ref_=fn_al_tt_10" >Luther</a> (1968) (TV Movie) ']

不要试图用正则表达式处理HTML,而是使用DOM解析器。应该是python的一个很好的起点。问题是您的。*?不匹配换行符。如果启用单线模式s,它将按预期工作。@rawing啊谢谢,使用[\s\s]*?要匹配任何字符、空白字符和非空白字符!谢谢你事实上这就是我昨天尝试过的:result=re.findallr'REGEX',stresult,flags=re.DOTALL但它不起作用,也许我失败了。
src = '''<div class="findSection">
<h3 class="findSectionHeader"><a name="tt"></a>Titles</h3>
<table class="findList">
<tr class="findResult odd"> <td class="primary"> <a href="/title/tt1474684/?ref_=fn_al_tt_1" >
<img src="https://images-na.ssl-images-amazon.com/images/M/_AL_.jpg" />
</a> </td> <td class="result_text"> 
<a href="/title/tt1474684<a href="/title/tt3155298/?ref_=fn_al_tt_3" >
<img src="http://ia.media-imdb.com/imagestd class="primary_photo"> 
<a href="/tiopicture/32x44/film-3119741174._CB522736599_.png" /></a>
</td> <td class="result_text"> 
<a href="/title/tt1501661/?ref_=fn_al_tt_10" >Luther</a> (1968) (TV Movie) </td> </tr></table>'''

expr = r'<h3 class="findSectionHeader"><a name="tt"><\/a>Titles<\/h3>[\s]{0,3}(.*?)<\/td> <\/tr><\/table>'

import re

print re.findall( expr, src, re.DOTALL )
['<table class="findList">\n<tr class="findResult odd"> <td class="primary"> <a href="/title/tt1474684/?ref_=fn_al_tt_1" >\n<img src="https://images-na.ssl-images-amazon.com/images/M/_AL_.jpg" />\n</a> </td> <td class="result_text"> \n<a href="/title/tt1474684<a href="/title/tt3155298/?ref_=fn_al_tt_3" >\n<img src="http://ia.media-imdb.com/imagestd class="primary_photo"> \n<a href="/tiopicture/32x44/film-3119741174._CB522736599_.png" /></a>\n</td> <td class="result_text"> \n<a href="/title/tt1501661/?ref_=fn_al_tt_10" >Luther</a> (1968) (TV Movie) ']