在tbody中获取tr的内容

Sandra Guilep Zouaoui Zandeh

我有下表:

<table class="table table-bordered adoption-status-table">
        <thead>
            <tr>
                <th>Extent of IFRS application</th>
                <th>Status</th>
                <th>Additional Information</th>
            </tr>
        </thead>
        <tbody>
                    <tr>
                        <td>IFRS Standards are required for domestic public companies</td>
                        <td>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>IFRS Standards are permitted but not required for domestic public companies</td>
                        <td>
                                <img src="/images/icons/tick.png" alt="tick">
                        </td>
                        <td>Permitted, but very few companies use IFRS Standards.</td>
                    </tr>
                    <tr>
                        <td>IFRS Standards are required or permitted for listings by foreign companies</td>
                        <td>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>The IFRS for SMEs Standard is required or permitted</td>
                        <td>
                                <img src="/images/icons/tick.png" alt="tick">
                        </td>
                        <td>The IFRS for SMEs Standard is permitted, but very few companies use it. Nearly all SMEs use Paraguayan national accounting standards.</td>
                    </tr>
                    <tr>
                        <td>The IFRS for SMEs Standard is under consideration</td>
                        <td>
                        </td>
                        <td></td>
                    </tr>
        </tbody>
    </table>

我试图像原始数据一样提取数据

在此处输入图片说明

这是我的工作:

from bs4 import BeautifulSoup
import requests
import pandas as pd
import re
# Site URL
url = "https://www.ifrs.org/use-around-the-world/use-of-ifrs-standards-by-jurisdiction/paraguay"
# Make a GET request to fetch the raw HTML content
html_content = requests.get(url).text
# Parse HTML code for the entire site
soup = BeautifulSoup(html_content, "lxml")
gdp = soup.find_all("table", attrs={"class": "adoption-status-table"})
print("Number of tables on site: ",len(gdp))

table1 = gdp[0]
body = table1.find_all("tr")
head = body[0] 
body_rows = body[1:] 

headings = []
for item in head.find_all("th"):
    item = (item.text).rstrip("\n")
    headings.append(item)
print(headings)

all_rows = [] 
for row_num in range(len(body_rows)): 
    row = [] 
    for row_item in body_rows[row_num].find_all("td"):
        aa = re.sub("(\xa0)|(\n)|,","",row_item.text)
        row.append(aa)
    all_rows.append(row)

df = pd.DataFrame(data=all_rows,columns=headings)

这是我得到的唯一输出:

Number of tables on site:  1
['Extent of IFRS application', 'Status', 'Additional Information']

我想将NULL单元格替换为False,并将图像的路径替换为True。

Danila Ganchar

您需要在中寻找img元素td这是一个例子:

data = []
for tr in body_rows:
    cells = tr.find_all('td')
    img = cells[1].find('img')
    if img and img['src'] == '/images/icons/tick.png':
        status = True
    else:
        status = False
    
    data.append({
        'Extent of IFRS application': cells[0].string,
        'Status': status,
        'Additional Information': cells[2].string,
    })

print(pd.DataFrame(data).head())

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在tbody表jquery中的第一个tr标签之后清除所有tr标签

来自分类Dev

如何从数组中获取内容?

来自分类Dev

如何使用HtmlAgilityPack获取tr链接和内容?

来自分类Dev

在tbody的<tr>标记内获取输入?

来自分类Dev

从改造中获取内容

来自分类Dev

获取事件中的按钮内容

来自分类Dev

在table.th.tr和table.tbody.tr中选择两种类型的表中的th

来自分类Dev

在BeautifulSoup中获取表的内容

来自分类Dev

从被嵌入的消息中获取内容?

来自分类Dev

在tbody中获取tr的内容

来自分类Dev

使用lxml在python中获取tr的tbody内的所有td内容

来自分类Dev

在动态获取的tr中添加图像按钮

来自分类Dev

在Summernote中获取选定的内容

来自分类Dev

烧瓶从内容中获取状态?

来自分类Dev

无法通过Googlesheet上的importxml从tbody html获取内容

来自分类Dev

如何从内容中获取数字

来自分类Dev

在tbody表jquery中的第一个tr标签之后清除所有tr标签

来自分类Dev

如何从数组中获取内容?

来自分类Dev

如何使用HtmlAgilityPack获取tr链接和内容?

来自分类Dev

如何在tbody中获取所有标签?

来自分类Dev

获取事件中的按钮内容

来自分类Dev

从表中获取tr内的td值

来自分类Dev

在ActionLink中获取模型内容

来自分类Dev

TBody tr为空时删除TFoot

来自分类Dev

获取ID,tbody,tr,title中的所有全局属性值

来自分类Dev

从“append(<tr><td="ID">...”中获取ID

来自分类Dev

使用jquery获取tbody中第一行的id

来自分类Dev

如何从表 tr td 中获取值

来自分类Dev

获取 tbody td 的值