0

我正在尝试id='revSAR'使用 Python 正则表达式从下面的 HTML 标记中获取所有 URL:

<a id='revSAR' href='http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp_see_all_summary?ie=UTF8&showViewpoints=1&sortBy=byRankDescending' class='txtsmall noTextDecoration'>
  See all 136 customer reviews
</a>

我尝试了下面的代码,但它不起作用(它什么也没打印):

regex = b'<a id="revSAR" href="(.+?)" class="txtsmall noTextDecoration">(.+?)</a>'
pattern=re.compile(regex)
rev_url=re.findall(pattern,txt)
print ('reviews url: ' + str(rev_url))
4

4 回答 4

1

你可以尝试类似的东西

(_, url), = re.findall(r'href=([\'"]*)(\S+)\1', input)
print url

但是,我个人更愿意使用像BeautifulSoup这样的 HTML 解析库来完成这样的任务。

于 2013-08-20T05:55:42.513 回答
0

描述

该表达式将:

  • 找到锚标签
  • 要求锚标记具有带值的 id 属性revSAR
  • 将捕获 href 属性值,不包括任何周围的引号(如果存在)
  • 将捕获内部文本,并修剪空白
  • 将允许属性以任何顺序出现
  • 允许属性有双引号、单引号或不带引号
  • 避免在模式匹配 html 时经常触发正则表达式的许多边缘情况

<a(?=\s|>)(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sid=(['"]?)revSAR\1(?:\s|>)) (?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\shref=(['"]?)(.*?)\2(?:\s|>))(?:[^>=]|='(?:[^']|\\')*'|="(?:[^"]|\\")*"|=[^'"][^\s>]*)*>\s*(.*?)\s*<\/a>

在此处输入图像描述

例子

Live Demo

示例文本

注意这里的前几个锚标签有一些非常困难的边缘情况。

<a onmouseover=' id="revSAR" ; href="http://www.NotYourURL.com" ; if (3 <href&& href="http://www.NotYourURL.com" && 6>3) { funRotate(href) ; } ; '  href='http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp_see_all_summary?ie=UTF8&showViewpoints=1&sortBy=byRankDescending' class='txtsmall noTextDecoration'>
  You shouldn't find me
</a>



<a onmouseover=' img = 10; href="http://www.NotYourURL.com" ; if (3 <href&& href="http://www.NotYourURL.com" && 6>3) { funRotate(href) ; } ; ' id='revSAR' href='http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp_see_all_summary?ie=UTF8&showViewpoints=1&sortBy=byRankDescending' class='txtsmall noTextDecoration'>
  See all 111 customer reviews
</a>


<a id='revSAR' href='http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp_see_all_summary?ie=UTF8&showViewpoints=1&sortBy=byRankDescending' class='txtsmall noTextDecoration'>
  See all 136 customer reviews
</a>

火柴

第 0 组获取整个锚标记
第 1 组获取围绕 id 属性的引号,稍后用于查找正确的结束引号
第 2 组获取围绕 href 属性的引号,稍后用于查找正确的结束引号
第 3 组获取 href属性值,不包括任何引号 第 4 组获取内部文本,不包括任何周围的空格

[0][0] = <a onmouseover=' img = 10; href="http://www.NotYourURL.com" ; if (3 <href&& href="http://www.NotYourURL.com" && 6>3) { funRotate(href) ; } ; ' id='revSAR' href='http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp_see_all_summary?ie=UTF8&showViewpoints=1&sortBy=byRankDescending' class='txtsmall noTextDecoration'>
  See all 111 customer reviews
</a>
[0][1] = '
[0][2] = '
[0][3] = http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp_see_all_summary?ie=UTF8&showViewpoints=1&sortBy=byRankDescending
[0][4] = See all 111 customer reviews


[1][0] = <a id='revSAR' href='http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp_see_all_summary?ie=UTF8&showViewpoints=1&sortBy=byRankDescending' class='txtsmall noTextDecoration'>
  See all 136 customer reviews
</a>
[1][1] = '
[1][2] = '
[1][3] = http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp_see_all_summary?ie=UTF8&showViewpoints=1&sortBy=byRankDescending
[1][4] = See all 136 customer reviews
于 2013-08-20T14:30:36.927 回答
0

你不需要匹配那些不必要的部分,比如id=...href=...试试这个:

regex = 'http://.*\'\s+'

于 2013-08-20T05:50:03.580 回答
0

首先,为什么你的正则表达式不起作用?在您的 html 中,属性使用单引号引起来,而在正则表达式中则使用双引号。你只需要关心 href 属性。尝试一些href=['"](.+?)['"]正则表达式,如果你使用忽略大小写开关会更好

但再次使用正则表达式解析 html 是一个非常糟糕的决定。请通过这个

于 2013-08-20T06:02:36.987 回答