【发布时间】: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