HTML parsing using lxml code
i have following HTML 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>
I am using lxml+python code to parse above HTML file. I want to retrieve "XYZ Consultancy Ltd" but I am not able to find out how to do this. So far my code is as follows:-
impor开发者_运维百科t 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()
You can use the CSS Selector +
, a direct adjacent combinator, to get at the <br>
preceding the text. Then, the target text is contained in its tail
attribute.
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
One way of doing this is to use XPath to find such an a
node, and check that the next two elements are span
and br
. If so, look at the tail
attribute of the br
element:
from lxml import etree
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
精彩评论