BS4按类别搜索_返回空 [英] BS4 Searching by Class_ Returning Empty

查看:52
本文介绍了BS4按类别搜索_返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过在 find_all('div')之后将bs4 .contents链接在一起来成功地抓取所需的数据,但这似乎很脆弱.我想直接转到按班级需要的标签,但是我的"class_ =搜索返回 None .

I currently am successfully scraping the data I need by chaining bs4 .contents together following a find_all('div'), but that seems inherently fragile. I'd like to go directly to the tag I need by class, but my "class_=" search is returning None.

我在下面的html上运行了以下代码,该代码返回 None :

I ran the following code on the html below, which returns None:

soup = BeautifulSoup(text)  # this works fine 

tag = soup.find(class_ = "loan-section-content")  # this returns None

也尝试过 soup.find('div',class_ ="loan-section-content")-也会返回 None .

我的html是:

<div class="loan-section">
    <div class="loan-section-title">
        <span class="text-light"> Some Text </span>
</div>
<div class="loan-section-content">
    <div class="row">
        <div class="col-sm-6">
            <strong>More text</strong>
            <br/>
            <strong>
                <a href="https://www.google.com/maps/place/Dakar,+Senegal/" target="_blank">Dakar</a>,&nbsp;Senegal
            </strong>

推荐答案

尝试一下

soup.find(attrs={'class':'loan-section-content'})
or
soup.find('div','loan-section-content')

attrs 将搜索属性

演示:

这篇关于BS4按类别搜索_返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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