Hoang Pham
Hoang Pham

Reputation: 6949

Beautiful Soup 4 CSS selector does not work the same way the tutorial show

I run the sample CSS selector codes from [Beautiful Soup 4 tutorial page][1], but the results are different, some give the correct result, some do not. In the website, they say it should work the same way in Python 2.7 and 3. I have Python 2.7 and install Beautiful Soup 4. Does anyone have the same issue?

from bs4 import BeautifulSoup
import urllib2

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

soup = BeautifulSoup(html_doc)

My test (of course I use the same html doc in the tutorial):

soup.select("#link1 ~ .sister")
[]

Their test:

soup.select("#link1 ~ .sister")
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie"  id="link3">Tillie</a>]

Click Here to See

Upvotes: 3

Views: 3278

Answers (1)

Nafiul Islam
Nafiul Islam

Reputation: 82460

I've figured out your problem. You are using a version of BeautifulSoup that is older than 4.3.2.

I just had 4.1.2 installed, and I ran your code. I had the same problem, I got an empty list, now that I've updated it to 4.3.2, I get the list of siblings again.

You can install it via pip, but you can also get the latest version from Pypi and download it.

Upvotes: 5

Related Questions