【问题标题】:BeautifulSoup Finding Class with Spaces?BeautifulSoup 找到带空格的类?
【发布时间】:2021-08-17 03:55:22
【问题描述】:

BeautifulSoup 和 python3.9 使用 lmxl 作为解析器,

我想从下图中找到<div, class="playlistAPI..."

但是当我执行x = soup.find("div", class_="playlistInterface") 并打印时,我得到:

<div class="playlistInterface" style="position: relative; width: 100%; height: 100%">
<div class="mwPlayerContainer player-out">
<div class="videoHolder"><div class="videoDisplay">
<video class="persistentNativePlayer" id="kaltura_player_" kentryid="1_o54seuc8" kpartnerid="1493231" kuiconfid="41642851" kwidgetid="1_3t8nz2gs" poster="data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%01%00%00%00%01%08%02%00%00%00%90wS%DE%00%00%00%01sRGB%00%AE%CE%1C%E9%00%00%00%09pHYs%00%00%0B%13%00%00%0B%13%01%00%9A%9C%18%00%00%00%07tIME%07%DB%0B%0A%17%041%80%9B%E7%F2%00%00%00%19tEXtComment%00Created%20with%20GIMPW%81%0E%17%00%00%00%0CIDAT%08%D7c%60%60%60%00%00%00%04%00%01'4'%0A%00%00%00%00IEND%AEB%60%82" preload="none">
</video>
</div></div> </div>
</div>

IE,它没有出现,我使用x.find("div", class_="playlistAPI medialistContainer unselectable k-vertical")找不到它

我尝试了以下thread 中的建议以及从那里链接的线程线索,但我没有运气。

有没有办法做到这一点?还是我遗漏了一些明显的东西?

Here is the URL

提前谢谢你。

【问题讨论】:

  • 如果可以的话可以提供网址
  • 只是因为您需要在类名中包含 Period . !您正在匹配 4 个不同的类,请使用 playlistAPI.medialistContainer.unselectable.k-vertical
  • 使用句点也返回无。我已经提供了网址。随意尝试一下。
  • @ACB_prgm 你想在那个 div 容器中找到 Lectures 的标题吗??
  • @BhavyaParikh 是的!我正在尝试获取每个讲座的标题和持续时间。

标签: html python-3.x xml beautifulsoup


【解决方案1】:

正如您在链接问题中已经解释的那样:CSS 类名称中的空格将标记分隔为多个 CSS 类。你可以做一个列表比较来找到属于所有类的标签:

x.find("div", class_=["playlistAPI", "medialistContainer", "undetectable", "k-vertical"])

【讨论】:

  • 我试过了,它返回无。此外,整个关卡的结果不包括该 div。
【解决方案2】:

我通过使用 selenium 的 webdriver 解决了这个问题,并在我的 GET 请求和创建汤之间休眠。

import time
from bs4 import BeautifulSoup as bs
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install(), options=options_)
driver.get(Kaltura_URL)
    time.sleep(2)
    content = driver.page_source.encode("utf-8").strip()
    soup = bs(content, "lxml")

    for item in soup.find_all("li"):
        print(item.text)

我真的不明白为什么会这样,除了我认为它“欺骗” URL 以为有实际用户访问 URL 而不是机器人。

如果您明白原因,请发表评论并告诉我!我想了解原因,而不仅仅是让事情发挥作用。

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 2016-11-25
    • 2017-06-05
    • 1970-01-01
    • 2016-02-26
    • 2014-10-12
    • 1970-01-01
    • 2016-07-06
    • 2013-11-17
    相关资源
    最近更新 更多