BS4按Class_搜索返回空

时间:2014-11-16 01:15:12

标签: python html web-scraping beautifulsoup

我目前正在通过在find_all('div')之后将bs4 .contents链接在一起来成功地抓取我需要的数据,但这看起来本质上很脆弱。我想直接去上课时我需要的标签,但是我的" class _ ="搜索正在返回None

我在下面的html上运行了以下代码,它返回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>

1 个答案:

答案 0 :(得分:1)

试试这个

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

attrs将搜索属性

演示: Demo