Reputation: 433
My question is related to usage of HTMLParser but on a bit of nast html code.
I have a file/webpage containing multiple html/css entries and somewhere in bewteen the lines i get this frequently repeated parts of html code i need to parse to extract some certain data.
For example:
1)
Number 66 to be extracted
Number 123456 to be extracted ftom this comment
<h3 class="s KB8NC">66.&hsbc;
<!--
<A name="simp123456" href="text.php?p=1&i_simp_z_boc_nb_sec=123456&i_simp_s_vitrazka=1">
-->
ristill advocka, sygint: SURVE/123-021/11-2/XX</h3>
And another frequent entries which show up in pairs:
2)
First entry to be ignored because of empty 'data'
Number 123456 to extract
<p class="monozzio"></p>
<p class="monozzio"><a href="text.php?p=1&pup;i_simp_z_boc_nb_sec=123456&pup;i_simp_s_vitrazka=1">monozzio...</a></p>
Here is my first class so far but it starts to exceed my skills, any help appreciated.
from HTMLParser import HTMLParser
class MyParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.recording = 0
self.data = []
def handle_starttag(self, tag, attributes):
if tag != 'p':
return
if self.recording:
self.recording += 1
return
for name, value in attributes:
if name == 'class' and value == 'monozzio':
break
else:
return
self.recording = 1
def handle_endtag(self, tag):
if tag == 'p' and self.recording:
self.recording -= 1
def handle_data(self, data):
if self.recording:
##############################
#here parse data to get 123456
##############################
self.data.append(data)
p = MyParser()
f = open('file.html', 'r')
htm = f.read()
p.feed(htm)
print '\n'.join(p.data)
p.close()
Upvotes: 0
Views: 364
Reputation: 2811
This is not directly a solution to your problem, but BeautifulSoup is a library that makes it easier to parse HTML.
Then you can do something like:
import re
from bs4 import BeautifulSoup
html = BeautifulSoup(your_html_content)
for link in html.find_all('p.monozzio a'): # use css selectors
href = link.get('href')
reg = re.compile('i_simp_z_boc_nb_sec=([0-9]+)')
nbrs = reg.findall(href) # regex to extract values
Note that I didn't test the code, it's just a general idea.
Upvotes: 4