【问题标题】:Extract columns within an XML tables using Beautiful Soup使用 Beautiful Soup 提取 XML 表中的列
【发布时间】:2017-06-15 06:24:18
【问题描述】:

我有多个表,如下面的 MySQL 数据转储表,​​每个表代表数据库中的一行。我将提取以下信息以便将其迁移到不同的数据库。

<table name="dashboard">
  <column name="id">1</column>
  <column name="timestamp">2009-10-09 15:10:30</column>
  <column name="config_offline">1</column>
  <column name="item1">0.00</column>
  <column name="item2">0.00</column>
</table>

<table name="orders">
  <column name="id">1</column>
  <column name="timestamp">2016-08-04 08:39:13</column>
  <column name="item">1</column>
  <column name="payment">Check</column>
  <column name="cost">175.00</column>
  <column name="paid">175.00</column>
  <column name="cancel">0</column>
  <column name="received">1</column>
</table>

这是我目前正在尝试的:

from bs4 import BeautifulSoup

with open("test.xml", "r") as markup:
    soup = BeautifulSoup(markup, "xml")

for row in soup.find_all('column'):
    print(row.text)
with open("test.xml", "r") as markup:
soup = BeautifulSoup(markup, "xml")
# And I also try this, but this doesn't work neither. 
for row in soup.find_all('table'):
    for c in row.find_all('column'):
       print(c.text)

目前这种方法的问题是我无法区分这两个表名。有没有办法分别从两个不同的表中提取信息?

【问题讨论】:

  • 更新后的问题有错别字,把最后一行改成print(c.text)

标签: python html xml beautifulsoup


【解决方案1】:

您可以通过特定的属性找到特定的表:

import bs4
div_test="""  
<table name="dashboard">
  <column name="id">1</column>
  <column name="timestamp">2009-10-09 15:10:30</column>
  <column name="config_offline">1</column>
  <column name="item1">0.00</column>
  <column name="item2">0.00</column>
</table>
<table name="orders">
  <column name="id">1</column>
  <column name="timestamp">2016-08-04 08:39:13</column>
  <column name="item">1</column>
  <column name="payment">Check</column>
  <column name="cost">175.00</column>
  <column name="paid">175.00</column>
  <column name="cancel">0</column>
  <column name="received">1</column>
</table>
"""
soup = bs4.BeautifulSoup(div_test)
table_dashboard = soup.find('table', {'name':"dashboard"})
table_orders = soup.find('table', {'name':"orders"})
print table_dashboard
print '\n'
print table_orders

输出会给你table_dashboardtable_orders

<table name="dashboard">
<column name="id">1</column>
<column name="timestamp">2009-10-09 15:10:30</column>
<column name="config_offline">1</column>
<column name="item1">0.00</column>
<column name="item2">0.00</column>
</table>


<table name="orders">
<column name="id">1</column>
<column name="timestamp">2016-08-04 08:39:13</column>
<column name="item">1</column>
<column name="payment">Check</column>
<column name="cost">175.00</column>
<column name="paid">175.00</column>
<column name="cancel">0</column>
<column name="received">1</column>
</table>

【讨论】:

    【解决方案2】:

    似乎很明显......首先迭代“table”标签,然后迭代其“column”标签上的每个“table”标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 2011-11-03
      • 2021-10-17
      • 2018-01-15
      • 2013-04-22
      • 1970-01-01
      • 2015-09-18
      相关资源
      最近更新 更多