Can’t modify the logic of a script to scrape the accurate fields from tables of varying lengths

I’m trying to create a script to scrape a few fields from tables based on their headers. The problem is that all the tables are not of the same length, along with their headers. Here are the HTML elements of the two tables with different lengths for your consideration.

from bs4 import BeautifulSoup

html_table1 = """
<thead>
<tr>
    <td rowspan="1" data-sort-key="survey_date_surrkey"> </td>
    <th scope="col" colspan="1" data-sort-key="cell-1"><span>2 Bedroom</span></th>
    <th scope="col" colspan="1" data-sort-key="cell-2"><span>3 Bedroom +</span></th>
    <th scope="col" colspan="1" data-sort-key="cell-3"><span>Total</span></th>
</tr>            
</thead>
<tbody>
<tr>
    <th scope="row" class="first-cell" data-field="survey_date_surrkey" data-value="0">2010 October</th>
    <td class="numericalData" data-field="cell-1" data-value="23">23</td>
    <td class="numericalData" data-field="cell-2" data-value="5">5</td>
    <td class="numericalData" data-field="cell-3" data-value="28">28</td>
</tr>
</tbody>
"""

html_table = """
<thead>
<tr>
    <td rowspan="1" data-sort-key="survey_date_surrkey"> </td>
    <th scope="col" colspan="2" data-sort-key="cell-1"><span>Bachelor</span></th>
    <th scope="col" colspan="2" data-sort-key="cell-3"><span>1 Bedroom</span></th>
    <th scope="col" colspan="2" data-sort-key="cell-5"><span>2 Bedroom</span></th>
    <th scope="col" colspan="2" data-sort-key="cell-7"><span>3 Bedroom +</span></th>
    <th scope="col" colspan="2" data-sort-key="cell-9"><span>Total</span></th>
</tr>            
</thead>
<tbody>
<tr>
    <th scope="row" class="first-cell" data-field="survey_date_surrkey" data-value="0">1990 October</th>
    <td class="numericalData" data-field="cell-1" data-value="0.0">0.0</td>
    <td class="" data-field="cell-2" data-value="b ">b </td>
    <td class="numericalData" data-field="cell-3" data-value="0.2">0.2</td>
    <td class="" data-field="cell-4" data-value="a ">a </td>
    <td class="numericalData" data-field="cell-5" data-value="0.0">0.0</td>
    <td class="" data-field="cell-6" data-value="b ">b </td>
    <td class="numericalData" data-field="cell-7" data-value="0.0">0.0</td>
    <td class="" data-field="cell-8" data-value="b ">b </td>
    <td class="numericalData" data-field="cell-9" data-value="0.1">0.1</td>
    <td class="" data-field="cell-10" data-value="a ">a </td>
</tr>
</tbody>
"""

soup = BeautifulSoup(html_table1,'html.parser')

headers = [header.text.strip() for header in soup.select('thead th')]
non_class_presence = [item.text.strip() for item in soup.select("tbody tr td[class='']")]
index_num = len(soup.select("td.numericalData"))

try:
    bachelor = soup.select("td.numericalData")[0].text
except IndexError: bachelor = ""
try:
    one_bed = soup.select("td.numericalData")[1].text
except IndexError: one_bed = ""
try:
    two_bed = soup.select("td.numericalData")[2].text
except IndexError: two_bed = ""
try:
    three_bed = soup.select("td.numericalData")[3].text
except IndexError: three_bed = ""
try:
    total_bed = soup.select("td.numericalData")[4].text
except IndexError: total_bed = ""
print(f"bachelor: {bachelor}none_bed: {one_bed}ntwo_bed: {two_bed}nthree_bed: {three_bed}ntotal_bed: {total_bed}")

When I run the script using the second html_table, I get the following output: (correct result)

bachelor: 0.0
one_bed: 0.2
two_bed: 0.0
three_bed: 0.0
total_bed: 0.1

However, the script produces errorneous results when I run it using html_table1: (wrong result)

bachelor: 23
one_bed: 5
two_bed: 28
three_bed: 
total_bed:

Correct output for html_table1:

bachelor: 
one_bed: 
two_bed: 23
three_bed: 5
total_bed: 28

How can I change the script’s logic such that, despite the table’s and its headers’ length, the results are accurately parsed?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật