【问题标题】:Beautiful soup, exact match when using "findAll()"美丽的汤,使用“findAll()”时完全匹配
【发布时间】:2018-05-26 09:11:34
【问题描述】:

我正在使用 python (3.5)、selenium (3.6) 和 beautiful soup (4.6) 抓取网站。 我用来查找某个 html 标记的代码如下:

descContainer=descContainers[0].findAll("div", {"class":"userHtml"})

不幸的是,我不仅找到了我的目标 div,它是:

<div class="userHtml">

还有以下 div,其类名部分由感兴趣的类名组成:

<div class="cept-threadUpdate-html toggleSect-hide--collapsed userHtml space--b-2 space--h-2" data-lightbox-xhr="{"name":"thread_updates"}" data-handler="lightbox-xhr emoticon-preview">

有没有办法只找到完全匹配而不是找到所有包含上述类名的类?

【问题讨论】:

    标签: python html selenium beautifulsoup


    【解决方案1】:

    你应该看看这个问题:BeautifulSoup webscraping find_all( ): finding exact match

    答案似乎是:

    descContainer = descContainers[0].find_all(lambda tag: tag.name == 'div' and 
                                   tag.get('class') == ['userHtml'])
    

    【讨论】:

      【解决方案2】:

      更简洁易读的CSS selector

      descContainers[0].select("div[class=userHtml]")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-28
        • 2013-06-29
        • 2021-01-15
        相关资源
        最近更新 更多