[英]How to extract the URL from this HTML tag?
我正在尝试使用Python正则表达式从下面的HTML标记中获取所有id='revSAR'
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))
您可以尝试类似
(_, url), = re.findall(r'href=([\'"]*)(\S+)\1', input)
print url
但是,我个人更愿意将HTML解析库(例如BeautifulSoup)用于此类任务。
您无需匹配那些不必要的部分,例如id=...
, href=...
,请尝试以下操作:
regex = 'http://.*\\'\\s+'
首先,为什么您的正则表达式不起作用? 在您的html中,属性用单引号引起来,而在regex中则用双引号引起。 而且您只需要关心href属性。 尝试使用诸如href=['"](.+?)['"]
作为正则表达式的东西,如果使用ignore case switch会更好
但是同样,使用正则表达式解析html是一个非常糟糕的决定。 请通过这个
此表示将:
revSAR
的id属性 <a(?=\\s|>)(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\\s>]*)*?\\sid=(['"]?)revSAR\\1(?:\\s|>)) (?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\\s>]*)*?\\shref=(['"]?)(.*?)\\2(?:\\s|>))(?:[^>=]|='(?:[^']|\\\\')*'|="(?:[^"]|\\\\")*"|=[^'"][^\\s>]*)*>\\s*(.*?)\\s*<\\/a>
现场演示
示范文本
请注意,这里的前几个锚定标签有一些非常困难的边缘情况。
<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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.