Разбор таблицы с красивым супом - PullRequest
0 голосов
/ 05 апреля 2020

У меня есть проблемы, связанные с анализом таблицы ниже html с использованием Beautifulsoup, когда я пытаюсь запустить эту часть своего кода, все, что я получаю, это ошибки нетипов, где, по сути, print (table) = None.

Что я делаю не так?

<table class="table table-borderless table-striped no-background clear-padding-first-child available-slots-mobile main-table clone">
    <thead>
        <tr>
            <th width="14%" class="text-left nowrap fixed-side">Session Date</th>
            <th width="14%" class="text-center">
                <b>1</b>
            </th>
            <th width="14%" class="text-center">
                <b>2</b>
            </th>
            <th width="14%" class="text-center">
                <b>3</b>
            </th>
            <th width="14%" class="text-center">
                <b>4</b>
            </th>
            <th width="14%" class="text-center">
                <b>5</b>
            </th>
            <th width="14%" class="text-center">
                <b>6</b>
            </th>
        </tr>
    </thead>
    <tbody class="tr-border-bottom">
        <tr>
            <th class="pb-15 text-left fixed-side">
                <a href="javascript:changeDate('10 Jun 2020');">10 Jun 2020</a>
                <br> Wednesday
            </th>

            <td class="pb-15 text-center">
                <a href="#" id="1217428_1_10/6/2020 12:00:00 AM" class="slotBooking">
                                                                        8:15 AM ✔
                                                                    </a>
            </td>
    </tbody>
</table>

Это то, что я придумал до сих пор, но, кажется, он вообще не распознает таблицу? Я использую неправильный класс?

lesson_time = []
soup = bs(r.text, "html.parser")
table = soup.find("table" , {"class" : "table table-borderless table-striped"})
rows = table.findAll("tr")

for tr in rows:
    columns = rows.findAll("td")
    for td in columns:
        lesson_time.append(td.get_text())

print (lesson_time)  

1 Ответ

0 голосов
/ 05 апреля 2020
from bs4 import BeautifulSoup
import re

html = """
<table class="table table-borderless table-striped no-background clear-padding-first-child available-slots-mobile main-table clone">
    <thead>
        <tr>
            <th width="14%" class="text-left nowrap fixed-side">Session Date</th>
            <th width="14%" class="text-center">
                <b>1</b>
            </th>
            <th width="14%" class="text-center">
                <b>2</b>
            </th>
            <th width="14%" class="text-center">
                <b>3</b>
            </th>
            <th width="14%" class="text-center">
                <b>4</b>
            </th>
            <th width="14%" class="text-center">
                <b>5</b>
            </th>
            <th width="14%" class="text-center">
                <b>6</b>
            </th>
        </tr>
    </thead>
    <tbody class="tr-border-bottom">
        <tr>
            <th class="pb-15 text-left fixed-side">
                <a href="javascript:changeDate('10 Jun 2020');">10 Jun 2020</a>
                <br> Wednesday
            </th>

            <td class="pb-15 text-center">
                <a href="#" id="1217428_1_10/6/2020 12:00:00 AM" class="slotBooking">
                                                                        8:15 AM ✔
                                                                    </a>
            </td>
    </tbody>
</table>
"""

soup = BeautifulSoup(html, 'html.parser')
target = soup.find("table", class_=re.compile("^table table-borderless"))

items = [item.get_text(strip=True) for item in target.findAll(
    "td", class_="pb-15 text-center")]

print(items)

Выход:

['8:15 AM ✔']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...