Python - Использование bs4 для анализа указанных c данных из html - PullRequest
0 голосов
/ 30 мая 2020

Я все еще новичок в python и учусь, как go, поэтому, пожалуйста, потерпите меня .. Как заставить bs4 получать только данные 'value = * number *' из предоставленного кода?

<div class="option-radios">
<label class="radio option-7">
    <em style="display: none"></em>

    <span class="radio-label">7</span>
    <input id="option-7"
            type="radio" name="option[1]"
            class=" property-uid1"
            value="59"              title="7" />
</label>

<label class="radio option-8">
    <em style="display: none"></em>

    <span class="radio-label">8</span>
    <input id="option-8"
            type="radio" name="option[1]"
            class=" property-uid1"
            value="61"              title="8" />
</label>

<label class="radio option-9">
    <em style="display: none"></em>

    <span class="radio-label">9</span>
    <input id="option-9"
            type="radio" name="option[1]"
            class=" property-uid1"
            value="62"              title="9" />
</label>

<label class="radio option-10">
    <em style="display: none"></em>

    <span class="radio-label">10</span>
    <input id="option-10"
            type="radio" name="option[1]"
            class=" property-uid1"
            value="63" checked="checked"                title="10" />
</label>

<label class="radio option-11">
    <em style="display: none"></em>

    <span class="radio-label">11</span>
    <input id="option-11"
            type="radio" name="option[1]"
            class=" property-uid1"
            value="60"              title="11" />
</label>

Я могу получить все данные в div, но не могу сузить поиск до того, что мне нужно.

import requests
from bs4 import BeautifulSoup as bs

session = requests.session()
url = "url here"

a = session.get(url)

soup = bs(a.text,'html.parser')
sizes = soup.find('div',{'class':'option-radios'})
print(sizes)

Еще раз извиняюсь за, вероятно, очень basi c вопрос!

1 Ответ

0 голосов
/ 30 мая 2020

В этом случае вам, вероятно, будет лучше с селекторами css:

for val in soup.select('label input'):
    print(val['value'])

Вывод (для фрагмента в вашем вопросе):

59
61
62
63
60
...