
Untitled
By: a guest on
Jun 22nd, 2012 | syntax:
None | size: 1.43 KB | hits: 14 | expires: Never
HTML parsing using lxml code
<table class="results">
<tr>
<td>
<a href="..">link</a><span>2nd Mar 2011</span><br>XYZ Consultancy Ltd<br>
<div>....</div>
</td>
</tr>
</table>
import lxml.html
for el in root.cssselect("table.results"):
for el2 in el: #tr tags
for e13 in el2:#td tags
for e14 in e13:
if ( e14.tag == 'a') :
print "keyword: ",e14.text_content()
if (e14.tag == 'span'):
print "date: ",e14.text_content()
import lxml.html
root = lxml.html.fromstring('''
<table class="results">
<tr>
<td>
<a href="..">link</a><span>2nd Mar 2011</span><br>XYZ Consultancy Ltd<br>
<div>....</div>
</td>
</tr>
</table>
''')
for br_with_tail in root.cssselect('table.results > tr > td > a + span + br'):
print br_with_tail.tail
# => XYZ Consultancy Ltd
data = '''<table class="results">
<tr>
<td>
<a href="..">link</a><span>2nd Mar 2011</span><br>XYZ Consultancy Ltd<br>
<div>....</div>
</td>
</tr>
</table>'''
root = etree.HTML(data)
for e in root.xpath('//table[@class="results"]/tr/td/a'):
parsed_tag = e.text
next = e.getnext()
if next is None or next.tag != 'span':
continue
parsed_date = next.text
next_next = next.getnext()
if next_next is None or next_next.tag != 'br':
continue
print 'tag: ', parsed_tag
print 'date: ', parsed_date
print 'company: ', next_next.tail