def parse_html(html_string):
import re
fields = re.findall(r'(?<=\<)[\w=\s\"\']+?(?=\/?\>)', html_string)
out = []
for field in fields:
tag = re.match(r'(?P<tag>\w+?) ?', field).group('tag')
attrs = re.findall(r' (\w+?)\=[\"\'](.+?)[\"\']', field)
if attrs:
for x in attrs:
out.append(','.join([tag] + list(x)))
else:
out.append(tag)
print '\n'.join(out)
>>> from lxml import etree
>>> parser = etree.XMLParser(remove_blank_text=True)
>>> parsed_xml = etree.XML(s,parser)
>>> for i in parsed_xml.iter('*'):
... print i.tag
... for x in i.items():
... print '%s,%s' % (x[0],x[1])
...
r
P
color
val,1F497D
t
val,123
val2,234
我不太清楚您为什么想要这个,但是您应该看看Python的lxml或BeautifulSoup。你知道吗
或者,如果您只希望它完全符合您上面介绍的形式:
这有点过头了,这就是为什么您通常应该使用lxml或BeautifulSoup,但它可以完成这个特定的工作。你知道吗
以上程序输出:
安装^{} 然后:
我让你来格式化输出。你知道吗
我认为你最好的办法是使用BeautifulSoup
例如(从他们的docs):
你也可以看看lxml,它简单高效,这就是BeautifulSoup的基础。具体来说,您可能想看看this page。你知道吗
相关问题 更多 >
编程相关推荐