Python BS4按类搜索\返回空

Python BS4按类搜索\返回空,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,目前,我通过在find_all('div')之后将bs4.contents链接在一起,成功地抓取了所需的数据,但这似乎天生就很脆弱。我想按类直接转到我需要的标记,但我的“class\uu=“search”返回None 我在下面的html上运行了以下代码,返回None: soup = BeautifulSoup(text) # this works fine tag = soup.find(class_ = "loan-section-content") # this returns No

目前,我通过在
find_all('div')
之后将bs4.contents链接在一起,成功地抓取了所需的数据,但这似乎天生就很脆弱。我想按类直接转到我需要的标记,但我的“class\uu=“search”返回
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>

一些文本
更多文本

塞内加尔
试试这个

soup.find(attrs={'class':'loan-section-content'})
or
soup.find('div','loan-section-content')
attrs
将搜索属性

演示:

这是您正在使用的完整HTML吗?(至少,结尾缺少结束标记)无论如何,我已经尝试了所有的解析器-没有问题,它返回所需的
div
。请出示您目前拥有的完整代码<代码>文本可能与您展示的内容有所不同。工作正常,idk为什么不适合您谢谢大家的快速回复。我将上面的html完全保存到一个名为testhtml.txt的文件中。然后,我在f.open()和f.close()之间运行了上面的代码,但是(现在仍然)没有得到任何结果。但是,下面Hackaholic提供的两行代码都有效。这两行代码都适用于BeautifulSoup 4.1.0,并且当元素还有其他类时,它也会返回有问题的元素,例如
。在BeautifulSoup 4.2.1中,该问题已得到修复,
通过
类查找
-ing可以正常工作。幸运的是,4.1.0解决方案继续在4.2.1中工作。