質問

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