<html>
<table border="1px">
<tr>
<td>yes</td>
<td>no</td>
</tr>
</table>
</html>
Is there any way to get the contents of the table (yes ,no) besides beautifulsoup??
A python beginner,any help or any kind of direction will be of great help.
Thank you
You can use the HTMLParser
module that comes with the Python standard library.
>>> import HTMLParser
>>> data = '''
... <html>
... <table border="1px">
... <tr>
... <td>yes</td>
... <td>no</td>
... </tr>
... </table>
... </html>
... '''
>>> class TableParser(HTMLParser.HTMLParser):
... def __init__(self):
... HTMLParser.HTMLParser.__init__(self)
... self.in_td = False
...
... def handle_starttag(self, tag, attrs):
... if tag == 'td':
... self.in_td = True
...
... def handle_data(self, data):
... if self.in_td:
... print data
...
... def handle_endtag(self, tag):
... self.in_td = False
...
>>> p = TableParser()
>>> p.feed(data)
yes
no
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With