使用BeautifulSoup从表中解析数据

时间:2015-09-01 17:47:58

标签: python parsing beautifulsoup

我是BeautifulSoup的新手,我一直在努力从表中解析数据:

<table id="data">
    <tr>
      <td class="random.data"></td>
      <td class="name"></td>
      <td class="values"></td> <!-- 0 -->
      <td class="values"></td> <!-- 1 -->
      <td class="values"></td> <!-- 2 -->
      <td class="values"></td> <!-- 3 -->
    </tr>
    <tr>
      <td class=".random_data"></td>
      <td class="name"></td>
      <td class="values"></td> <!-- 0 -->
      <td class="values"></td> <!-- 1 -->
      <td class="values"></td> <!-- 2 -->
      <td class="values"></td> <!-- 3 -->
    </tr>
</table>

我想创建一个像这个伪代码的字典列表:

content = []
for tr in trs:
    info = {
        'name': tr.getChildren('.name').getText(),
        'value1': tr.getChildren('.values', 0).getText() # the first value from values
        'value3': tr.getChildren('.values', 3).getText() # the fourth value from values
    }
    content.append(info)

但是我一直在努力尝试将其转化为BeautifulSoup,任何帮助或提示?

1 个答案:

答案 0 :(得分:1)

我们的想法是遍历表行,并且对于任何行,按类名查找name,按values类名查找所有值,并按索引获取所需的值: / p>

from bs4 import BeautifulSoup

data = """
<table id="data">
    <tr>
      <td class="random.data"></td>
      <td class="name">test1</td>
      <td class="values">0</td>
      <td class="values">1</td>
      <td class="values">2</td>
      <td class="values">3</td>
    </tr>
    <tr>
      <td class=".random_data"></td>
      <td class="name">test2</td>
      <td class="values">0</td> 
      <td class="values">1</td> 
      <td class="values">2</td> 
      <td class="values">3</td>
    </tr>
</table>
"""

soup = BeautifulSoup(data)

data = []
for row in soup.select("table#data tr"):
    name = row.find("td", class_="name").get_text(strip=True)
    values = row.find_all("td", class_="values")

    data.append({
        "name": name,
        "value1": values[0].get_text(strip=True),
        "value3": values[3].get_text(strip=True)
    })

print data

打印:

[
    {'value3': u'3', 'name': u'test1', 'value1': u'0'}, 
    {'value3': u'3', 'name': u'test2', 'value1': u'0'}
]