BS4 Searching by Class_ Returning Empty

吃可爱长大的小学妹 提交于 2021-02-11 18:16:12

问题


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.

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

Also tried soup.find('div', class_ = "loan-section-content") - also returns None.

My html is:

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


try this

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

attrs will search on attributes

Demo: Demo



来源:https://stackoverflow.com/questions/26952723/bs4-searching-by-class-returning-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!