是否可以使用一个正则表达式来捕获href中的所有信息?
例如:
<div id="w1">
<ul id="u1">
<li><a id='1' href='book'>book<sup>1</sup></a></li>
<li><a id='2' href='book-2'>book<sup>2</sup></a></li>
<li><a id='3' href='book-3'>book<sup>3</sup></a></li>
</ul>
</div>我想要book,book-2和book-3。
发布于 2014-04-24 08:54:47
简短而简单:
html = '<div id="w1"><ul id="u1"><li><a id='1' href='book'>book<sup>1</sup></a></li><li><a id='2' href='book-2'>book<sup>2</sup></a></li><li><a id='3' href='book-3'>book<sup>3</sup></a></li></ul></div>'
result = re.findall("href='(.*?)'", html)解释:
Match the character string “href='” literally (case sensitive) «href='»
Match the regex below and capture its match into backreference number 1 «(.*?)»
Match any single character that is NOT a line break character (line feed) «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “'” literally «'»发布于 2014-04-24 08:55:28
您可以使用下面的regex来实现这一点
<a id='\d+' href='([\w-]+)'
import re
s = '''<div id="w1"><ul id="u1"><li><a id='1' href='book'>book<sup>1</sup></a></li><li><a id='2' href='book-2'>book<sup>2</sup></a></li><li><a id='3' href='book-3'>book<sup>3</sup></a></li></ul></div>'''
>>> print re.findall(r"<a id='\d+' href='([\w-]+)'", s)
['book', 'book-2', 'book-3']发布于 2014-04-24 11:42:43
使用自定义类扩展HTMLParser
class MyHTMLParser(HTMLParser):
def __init__(self,*args,**kw):
super().__init__(*args,**kw)
self.anchorlist=[]
def handle_starttag(self,tag,attrs):
if tag == 'a':
for attribute in attrs:
if attribute[0] == 'href':
self.anchorlist.append(attribute[1])这将把所有的URL放在anchorlist中。
顺便说一下,它是在Python3.x中
https://stackoverflow.com/questions/23264172
复制相似问题