6

I parse HTML with Python.

After parsing I search for some elements in the tree.

I found no easy to use way to find elements in the tree up to now. XPath is available, but I prefer a familiar way.

Is there a way to use selectors in Python which have a syntax similar to jquery/css selectors?

1

2 Answers 2

5

BeautifulSoup has CSS selectors support built-in:

>>> from bs4 import BeautifulSoup
>>> from urllib2 import urlopen
>>> soup = BeautifulSoup(urlopen("https://google.com"))
>>> soup.select("input[name=q]")
[<input autocomplete="off" class="lst" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top" title="Google Search" value=""/>]

There is also cssselect package that you can use in combination with lxml.

Note that there are certain limitations in how CSS selectors work in BeautifulSoup - lxml+csselect support more CSS selectors:

This is all a convenience for users who know the CSS selector syntax. You can do all this stuff with the Beautiful Soup API. And if CSS selectors are all you need, you might as well use lxml directly: it’s a lot faster, and it supports more CSS selectors. But this lets you combine simple CSS selectors with the Beautiful Soup API.

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

6 Comments

I get: AttributeError: 'lxml.etree._Element' object has no attribute 'cssselect' I use lxml version 3.3.3
@guettli could you update to 3.4.4 and try again? Also, what code are you executing?
This is a new question: See stackoverflow.com/questions/32264533/…
@guettli yeah, lxml.html has CSS selecting feature. If you are parsing html, you should use lxml.html and not lxml.etree.
I don't know if this is new, but now I have to install cssselect through pip in order to follow this answer
|
0

There is library called pyquery: https://pypi.python.org/pypi/pyquery

Here is an example from the docs:

>>> d = pq("<option value='1'><option value='2'>")
>>> d('option[value="1"]')
[<option>]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.