【问题标题】:Find element with multiple classes using BeautifulSoup使用 BeautifulSoup 查找具有多个类的元素
【发布时间】:2022-01-12 07:19:21
【问题描述】:

如何获取 h1 文本“Mini Militia - Doodle Army 2 apk” https://www.apkmonk.com/app/com.appsomniacs.da2/

我试过了,但没有成功

title = soup.find('div', class_='col l8  s8')  

请注意,页面上有多个具有“hide-on-med-and-down”和“hide-on-large-only”类的元素

<div class="col l8  s8">
        <h1 class="hide-on-med-and-down" style="font-size: 2em;">Mini Militia - Doodle Army 2 apk</h1>
        <h1 class="hide-on-large-only" style="font-size: 1.5em;margin:0px; padding: 0px;">Mini Militia - Doodle Army 2 apk</h1>
        <p class="hide-on-small-only" style="font-size: 1.2em;"><span class="item" style="display:none !important;"><span class="fn">Download Mini Militia - Doodle Army 2 APK Latest Version</span></span><b>App Rating</b>: <span class="rating"><span class="average">4.1</span>/<span class="best">5</span></span></p>
        <a class="hide-on-med-and-up" onclick="ga('send', 'event', 'nav', 'similar_mob_link', 'com.appsomniacs.da2');" style="font-size: 1.2em;" href="#similar">(<b>Similar Apps</b>)</a>
</div>

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    这会对你有所帮助。

    title = soup.find("h1", {'class':'hide-on-med-and-down'}).text
    

    【讨论】:

    • 页面上有多个元素具有此类'hide-on-med-and-down'
    • 在给定的网页中只有一个这样的标签可用。我将更新多个标签的代码
    【解决方案2】:

    会发生什么?

    本站有两个&lt;h1&gt;,内容相同,只是类名不同,要控制大小,...应该以不同的分辨率显示

    如何解决?

    原因内容相同,只需选择树中的第一个&lt;h1&gt; 即可获得结果,在这种情况下类名无关紧要,原因结果始终相同:

    title = soup.find('h1').text
    

    输出

    Mini Militia - Doodle Army 2 apk
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多