2

New to this library (no more familiar with BeautifulSoup either, sadly), trying to do something very simple (search by inline style):

<td style="padding: 20px">blah blah </td>

I just want to select all tds where style="padding: 20px", but I can't seem to figure it out. All the examples show how to select td, such as:

for col in page.cssselect('td'):

but that doesn't help me much.

3 Answers 3

4

Well, there's a better way: XPath.

import lxml.html
data = """<td style="padding: 20px">blah blah </td>
<td style="padding: 21px">bow bow</td>
<td style="padding: 20px">buh buh</td>
"""
doc = lxml.html.document_fromstring(data)
for col in doc.xpath("//td[@style='padding: 20px']"):
    print col.text

That is neater and also faster.

Sign up to request clarification or add additional context in comments.

Comments

3

If you prefer to use CSS selectors:

import lxml.html
data = """<td style="padding: 20px">blah blah </td>
<td style="padding: 21px">bow bow</td>
<td style="padding: 20px">buh buh</td>
"""
doc = lxml.html.document_fromstring(data)
for td in doc.cssselect('td[style="padding: 20px"]'):
   print td.text

Comments

2

Note that both Ruslan Spivak and nosklo have given better answers below.


import lxml.html
data = """<td style="padding: 20px">blah blah </td>
<td style="padding: 21px">bow bow</td>
<td style="padding: 20px">buh buh</td>
"""
doc = lxml.html.document_fromstring(data)
for col in doc.cssselect('td'):
    style = col.attrib['style']
    if style=='padding: 20px':
        print(col.text.strip())

prints

blah blah
buh buh

and manages to skip bow bow.

3 Comments

Thanks! Now all I need is for lxml to actually install on a windows machine, and I'm golden!
Why document_fromstring not just fromstring?
@nn0p: document_fromstring returns a HtmlElement which begins with <html>, fromstring returns a HtmlElement which begins with <div>. In this case it does not matter.

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.