Split string with commas also splits ampersands
-
01-06-2021 - |
문제
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.
제휴하지 않습니다 StackOverflow