1

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:-

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()
1
  • Why don't you use an XPath expression here? Commented Apr 13, 2011 at 8:03

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.