了解Beautiful Soup中的Find()函数 [英] Understand the Find() function in Beautiful Soup

查看:137
本文介绍了了解Beautiful Soup中的Find()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我想做的事情很简单,但这却使我感到悲伤.我想使用BeautifulSoup从HTML中提取数据.为此,我需要正确使用.find()函数.这是我正在使用的HTML:

I know what I'm trying to do is simple but it's causing me grief. I'd like pull data from HTML using BeautifulSoup. To do that I need to properly use the .find() function. Here's the HTML I'm working with:

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

我想要的值是73599中的73599data-value=32452中的32352percentage good中的69%.

The values I want are 73599 from data-value=73599, 32352 from data-value=32452, and the 69% from percentage good.

使用过去的代码和在线示例,这是我到目前为止的内容:

Using past code and online examples, this is what I have so far:

RealValue = soup.find("div", {"class":"real number"})['data-value']
FakeValue = soup.find("audit", {"class":"fake number"})['data-value']

到目前为止,两者均无效.我不确定如何制作查找内容以提取69%号.

Both so far to no effect. I'm not sure how to craft the find in order to pull the 69% number.

推荐答案

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

在这里您要搜索div元素,但是span在示例HTML数据中具有实数"类,请尝试:

Here you are searching for a div element, but the span has the "real number" class in your example HTML data, try instead:

soup.find("span", {"class": "real number", "data-value": True})['data-value']

在这里,我们还在检查data-value属性的存在.

Here we are also checking for presence of data-value attribute.

要查找具有实数"或伪数"类的元素,可以创建 CSS选择器:

To find elements having "real number" or "fake number" classes, you can make a CSS selector:

for elm in soup.select(".real.number,.fake.number"):
    print(elm.get("data-value"))


要获取69%值:

soup.find("div", {"class": "percentage good"}).get_text(strip=True)

或者,一个CSS选择器:

Or, a CSS selector:

soup.select_one(".percentage.good").get_text(strip=True)
soup.select_one(".score .percentage").get_text(strip=True)

或者,找到具有Audit score文本的h6元素,然后获取先前的兄弟姐妹:

Or, locating the h6 element having Audit score text and then getting the preceding sibling:

soup.find("h6", text="Audit score").previous_sibling.get_text(strip=True)

这篇关于了解Beautiful Soup中的Find()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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