summaryrefslogtreecommitdiff
path: root/doc/source/index.rst
diff options
context:
space:
mode:
authorLeonard Richardson <leonard.richardson@canonical.com>2012-03-02 10:29:08 -0500
committerLeonard Richardson <leonard.richardson@canonical.com>2012-03-02 10:29:08 -0500
commit74ca8e3f33d44475401be0bc418da83264f91207 (patch)
tree329891346e0a4a9fb032666b7b36d42c44d1857f /doc/source/index.rst
parente3671b76b089f015ded142966aae0e8cdb572aa6 (diff)
Brought the soupselect port up to date.
Diffstat (limited to 'doc/source/index.rst')
-rw-r--r--doc/source/index.rst36
1 files changed, 31 insertions, 5 deletions
diff --git a/doc/source/index.rst b/doc/source/index.rst
index a9d404a..37d5f07 100644
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -1538,15 +1538,27 @@ You can find tags::
Find tags beneath other tags::
- soup.select("p a")
- # [<a class="sister" href="http://example.com/elsie"
- id="link1">Elsie</a>, <a class="sister"
- href="http://example.com/lacie" id="link2">Lacie</a>, <a
- class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
+ soup.select("body a")
+ # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
+ # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
+ # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup.select("html head title")
# [<title>The Dormouse's story</title>]
+Find tags `directly` beneath other tags::
+
+ soup.select("head > title")
+ # [<title>The Dormouse's story</title>]
+
+ soup.select("p > a")
+ # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
+ # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
+ # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
+
+ soup.select("body > a")
+ # []
+
Find tags by CSS class::
soup.select(".sister")
@@ -1590,6 +1602,20 @@ Find tags by attribute value::
soup.select('a[href*=".com/el"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
+Match language codes::
+
+ multilingual_markup = """
+ <p lang="en">Hello</p>
+ <p lang="en-us">Howdy, y'all</p>
+ <p lang="en-gb">Pip-pip, old fruit</p>
+ <p lang="fr">Bonjour mes amis</p>
+ """
+ multilingual_soup = BeautifulSoup(multilingual_markup)
+ multilingual_soup.select('p[lang|=en]')
+ # [<p lang="en">Hello</p>,
+ # <p lang="en-us">Howdy, y'all</p>,
+ # <p lang="en-gb">Pip-pip, old fruit</p>]
+
This is 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,