Python 了解Beautiful Soup中的Find()函数

Python 了解Beautiful Soup中的Find()函数,python,html,beautifulsoup,Python,Html,Beautifulsoup,我知道我想做的很简单,但这让我很伤心。我想使用BeautifulSoup从HTML中提取数据。为此,我需要正确使用.find()函数。以下是我正在使用的HTML: <div class="audit"> <div class="profile-info"> <img class="profile-pic" src="https://pbs.twimg.com/profile_images/471758097036226560/tLLeiOi

我知道我想做的很简单,但这让我很伤心。我想使用BeautifulSoup从HTML中提取数据。为此,我需要正确使用
.find()
函数。以下是我正在使用的HTML:

<div class="audit">

    <div class="profile-info">
        <img class="profile-pic" src="https://pbs.twimg.com/profile_images/471758097036226560/tLLeiOiL_normal.jpeg" />
        <h4>Ed Boon</h4>
        <span class="screen-name"><a href="http://www.twitter.com/noobde" target="_blank">@noobde</a></span>
    </div>

        <div class="followers">
            <div class="pie"></div>
            <div class="pie-data">
                <span class="real number" data-value=73599>73,599</span><span class="real"> Real</span><br />
                <span class="fake number" data-value=32452>32,452</span><span class="fake"> Fake</span><br />
                <h6>Followers</h6>
            </div>
        </div>
        <div class="score">
            <img src="//twitteraudit-prod.s3.amazonaws.com/dist/f977287de6281fe3e1ef36d48d996fb83dd6a876/img/audit-result-good.png" />
            <div class="percentage good">
                69%
            </div>
            <h6>Audit score</h6>
到目前为止,两者都没有效果。我不知道如何精心设计这个发现,以便提取
69%
号码

soup.find(“div”,“class”:“real number”})['data-value']

在这里,您正在搜索
div
元素,但是
span
在示例HTML数据中有“real number”类,请尝试:

soup.find("span", {"class": "real number", "data-value": True})['data-value']
这里我们还检查是否存在
数据值
属性


要查找具有“实数”或“假数”类的元素,可以进行以下操作:


要获取
69%
值:

soup.find("div", {"class": "percentage good"}).get_text(strip=True)
或者,CSS选择器:

soup.select_one(".percentage.good").get_text(strip=True)
soup.select_one(".score .percentage").get_text(strip=True)
或者,定位具有审核分数文本的
h6
元素,然后获取:


好极了这正是我想要的。结案。对于犯了与我相同错误的任何人,传入
soup.find()
的参数需要双引号。
soup.find("div", {"class": "percentage good"}).get_text(strip=True)
soup.select_one(".percentage.good").get_text(strip=True)
soup.select_one(".score .percentage").get_text(strip=True)
soup.find("h6", text="Audit score").previous_sibling.get_text(strip=True)