【问题标题】:How to find text match from "class" HTML label?如何从“类”HTML标签中查找文本匹配?
【发布时间】:2019-07-15 20:52:41
【问题描述】:

我需要解析 HTML 并找到匹配“product-size _product-size”的文本(没有任何其他词,如“disabled _disabled”)。 所以我使用 BeautifulSoup 并减少了我需要的 HTML 代码

import requests
from bs4 import BeautifulSoup
import re

URL =.......

headers = {"User-Agent": .......}

page = requests.get(URL, headers=headers)

soup = BeautifulSoup(page.content, 'html.parser')

div = soup.find("div", class_="size-list")
print("Find size-list \n" + str(div) +'\n')

知道了

<div class="size-list" tabindex="-1">
    <label for="size-10" 
    class="product-size _product-size disabled _disabled " 
    data-sku="01122345" data-name="10">
        <div>
            <input type="radio" value="10" name="size" id="size-10" 
            disabled="disabled" class="_sizeInput" tabindex="-1">
        </div>
        <span class="size-name" title="10">10</span>
        <span></span>
    </label>
    <label for="size-11" 
    class="product-size _product-size disabled _disabled " 
    data-sku="01122346" data-name="11">
        <div>
            <input type="radio" value="11" name="size" id="size-11" 
            disabled="disabled" class="_sizeInput" tabindex="-1">
        </div>
        <span class="size-name" title="11">11</span>
        <span></span>
    </label>
    <label for="size-12" 
    class="product-size _product-size " 
    data-sku="01122347" data-name="12">
        <div>
            <input type="radio" value="12" name="size" id="size-12" 
            class="_sizeInput" tabindex="0">
        </div>
        <span class="size-name" title="12">12</span>
        <span></span>
    </label>
    <label for="size-13" 
    class="product-size _product-size disabled _disabled " 
    data-sku="01122348" data-name="13">
        <div>
            <input type="radio" value="13" name="size" id="size-13" 
            disabled="disabled" class="_sizeInput" tabindex="-1">
        </div>
        <span class="size-name" title="13">13</span>
        <span></span>
    </label>
    <label for="size-14" 
    class="product-size _product-size " 
    data-sku="01122349" data-name="14">
        <div>
            <input type="radio" value="14" name="size" id="size-14" 
            class="_sizeInput" tabindex="0">
        </div>
        <span class="size-name" title="14">14</span>
        <span></span>
    </label>
</div>

现在我需要在文本中查找匹配字符串“product-size _product-size”而不是“disabled _disabled”的匹配项 如果我找到了,请检查他们有什么“尺寸名称”。 我只是卡住了(半小时 Python 用户,抱歉)。试图通过使用这个来找到与字符串“product-size _product-size”的简单匹配

soup.find_all('label', class_="product-size _product-size ")
soup.find(class_="product-size _product-size ")
soup.find_all(text=re.compile(r'product-size _product-size '))
#div.find... or soup.find..., and ect, whatever. 

但只有 [] 或无。 我做错了什么?

【问题讨论】:

    标签: python python-3.x beautifulsoup


    【解决方案1】:

    使用 Css 选择器和 :not(class)

    data='''<div class="size-list" tabindex="-1">
        <label for="size-10" 
        class="product-size _product-size disabled _disabled " 
        data-sku="01122345" data-name="10">
            <div>
                <input type="radio" value="10" name="size" id="size-10" 
                disabled="disabled" class="_sizeInput" tabindex="-1">
            </div>
            <span class="size-name" title="10">10</span>
            <span></span>
        </label>
        <label for="size-11" 
        class="product-size _product-size disabled _disabled " 
        data-sku="01122346" data-name="11">
            <div>
                <input type="radio" value="11" name="size" id="size-11" 
                disabled="disabled" class="_sizeInput" tabindex="-1">
            </div>
            <span class="size-name" title="11">11</span>
            <span></span>
        </label>
        <label for="size-12" 
        class="product-size _product-size " 
        data-sku="01122347" data-name="12">
            <div>
                <input type="radio" value="12" name="size" id="size-12" 
                class="_sizeInput" tabindex="0">
            </div>
            <span class="size-name" title="12">12</span>
            <span></span>
        </label>
        <label for="size-13" 
        class="product-size _product-size disabled _disabled " 
        data-sku="01122348" data-name="13">
            <div>
                <input type="radio" value="13" name="size" id="size-13" 
                disabled="disabled" class="_sizeInput" tabindex="-1">
            </div>
            <span class="size-name" title="13">13</span>
            <span></span>
        </label>
        <label for="size-14" 
        class="product-size _product-size " 
        data-sku="01122349" data-name="14">
            <div>
                <input type="radio" value="14" name="size" id="size-14" 
                class="_sizeInput" tabindex="0">
            </div>
            <span class="size-name" title="14">14</span>
            <span></span>
        </label>
    </div>'''
    soup=BeautifulSoup(data,'html.parser')
    for item in soup.select('.product-size._product-size:not(.disabled)'):
        print(item.select_one('.size-name').text)
    

    输出:

    12
    14
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-09
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多