美丽的汤4 CSS选择器不相同的方式工作教程节目 [英] Beautiful Soup 4 CSS selector does not work the same way the tutorial show

查看:177
本文介绍了美丽的汤4 CSS选择器不相同的方式工作教程节目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的样品CSS选择器的codeS由[美丽的汤4教程页] [1],但结果​​是不同的,有的给出正确的结果,有些则没有。在网站上,他们说应该以同样的方式在Python 2.7和3。我的Python 2.7和安装美丽的汤4。有没有人有同样的问题?

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)

我的测试(当然我用的是相同的HTML文档中的教程):

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

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

他们的测试:

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

点击这里查看

推荐答案

我已经想通了你的问题。您正在使用一个版本的BeautifulSoup是年纪比4.3.2。

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

我刚安装了4.1.2,和我跑你code。我有同样的问题,我得到了一个空表,现在我已经是更新到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.

您的可以的通过PIP安装它,但你也可以得到最新的一封来自PyPI 版本并下载。

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

这篇关于美丽的汤4 CSS选择器不相同的方式工作教程节目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆