The code below parses an HTML, the trouble is splitting when ampersands appear in the data.

from HTMLParser import HTMLParser

data = '<HTML><meta http-equiv="Pragma" content="no-cache"></head>'\
'<body>107,1,236,1000,70,498,NameA NameB & NameC - ActionA ActionB</body></html>'

class MyHTMLParser(HTMLParser):
      def handle_data(self, data):
            print data.split(',')

parser = MyHTMLParser()
parser.feed(data)

Output
It is splitting the '&' instead of only commas.

['107', '1', '236', '1000', '70', '498', 'NameA NameB ']
['&']
[' NameC - ActionA ActionB']

Thanks

有帮助吗?

解决方案

Well I think this is the way to go,

data2 = data.replace('&', 'and')

其他提示

An alternate solution, fetch value in <body> tag and parse using data.split(',') using Beautifulsoup or any library of your choice.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top