【问题标题】:How can I find a specific div tag using bs4如何使用 bs4 找到特定的 div 标签
【发布时间】:2021-05-08 04:20:53
【问题描述】:

我正在尝试解析 html 文本并找到所有“

”标签。
warpips = open('WarpipsPageText.txt', 'r')
page_text = warpips.read()
warpips.close()
bs4 = BeautifulSoup(page_text, 'html5lib')
div = bs4.find_all('div class="topclick-list-element-game-merchant"', bs4)
print(div)

当我运行此代码时,它会打印一个空列表。

下面是我试图隔离的 html 中的 sn-p。

        <div class="topclick-list-element-game-merchant">
            CDKeys.com
                                                <div class="platform platform-pc" title="pc"></div>
                                                                <div class="platform platform-xbox" title="xbox"></div>
                                    </div>
    </div>
    <span class="topclick-list-element-price">$11.19</span>
 </a>
                                                                                                 <a href="https://cheapdigitaldownload.com/nier-replicant-ver-1-22474487139-digital-download-price-comparison/" title="NieR Replicant ver.1.22474487139 cd key best prices" class="topclick-list-element ">
    <div class="topclick__image tpsprite11 tpsprite11-82-buy-nier-replicant-ver-1-22474487139-cd-key-pc-download-catalog-0" data-tp="tpsprite11"></div>
    <div class="topclick-list-element-game">
        <div class="topclick-list-element-game-title">NieR Replicant ver.1.22474487139</div>
        <div class="topclick-list-element-game-merchant">

【问题讨论】:

标签: python parsing web-scraping beautifulsoup


【解决方案1】:

你放错了一个单引号。

div = bs4.find_all('div', class="topclick-list-element-game-merchant")

【讨论】:

  • 这给了我这个错误:div = bs4.find_all('div', class="topclick-list-element-game-merchant")SyntaxError: invalid syntax
【解决方案2】:

要查找所有&lt;div&gt;class="topclick-list-element-game-merchant",您可以使用以下示例:

from bs4 import BeautifulSoup

html_doc = """
        <div class="topclick-list-element-game-merchant">
            CDKeys.com
                                                <div class="platform platform-pc" title="pc"></div>
                                                                <div class="platform platform-xbox" title="xbox"></div>
                                    </div>
    </div>
    <span class="topclick-list-element-price">$11.19</span>
 </a>
                                                                                                 <a href="https://cheapdigitaldownload.com/nier-replicant-ver-1-22474487139-digital-download-price-comparison/" title="NieR Replicant ver.1.22474487139 cd key best prices" class="topclick-list-element ">
    <div class="topclick__image tpsprite11 tpsprite11-82-buy-nier-replicant-ver-1-22474487139-cd-key-pc-download-catalog-0" data-tp="tpsprite11"></div>
    <div class="topclick-list-element-game">
        <div class="topclick-list-element-game-title">NieR Replicant ver.1.22474487139</div>
        <div class="topclick-list-element-game-merchant">
"""

soup = BeautifulSoup(html_doc, "html.parser")

for div in soup.find_all(class_="topclick-list-element-game-merchant"):
    print(div.get_text(strip=True))

打印:

CDKeys.com

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2021-03-18
    • 1970-01-01
    相关资源
    最近更新 更多