我正在编写一个带有python的HTML解析器,并且为了从我使用正则表达式的标签中提取HTML属性。这是我正在使用的表达式
tag_exp = r'</?(?P<name>[a-z A-Z]+) (?P<attribute>[a-z A-Z]+="[\w]+")* /?>'
matches = re.match(tag_exp, '<img src="test.jpg" alt="test">')
但它并不匹配任何东西。我一直试图弄清楚,我做错了什么?
答案 0 :(得分:0)
请改用BeautifulSoup。查看此示例。
import BeautifulSoup
html = '<p class="c4">SOMETEXT</p><p class="c5"></p>'
soup = BeautifulSoup.BeautifulSoup(html)
print [tag.attrs for tag in soup.findAll('p') if tag.string]