使用Python解析带有表的HTML文件

时间:2014-12-03 12:52:59

标签: python html parsing beautifulsoup html-parsing

我的python解析器出了问题。它是我文件的一部分:

<tr>
    <td class="zeit"><div>03.12. 10:45:00</div></td>
    <td class="system"><div><a target="_blank" href="detail.php?host=CG&factor=2&delay=1&Y=15">CG</div></a></td>
    <td class="fehlertext"><div>System steht nicht zur Verfügung!</div></td>
</tr>

<tr>
    <td class="zeit"><div>03.12. 10:10:01</div></td>
    <td class="system"><div><a target="_blank" href="detail.php?host=DEXProd&factor=2&delay=5&Y=15">DEX</div></a></td>
    <td class="fehlertext"><div>ssh: Connection refused Couldn't read packet: Connection reset by peer</div></td>
</tr>

<tr>
    <td class="zeit"><div>03.12. 06:23:06</div></td>
    <td class="system"><div><a target="_blank" href="detail.php?host=FRAUD&factor=2&delay=1&Y=15">Boni</div></a></td>
    <td class="fehlertext"><div>ID Fehler</div></td>
</tr>

现在我将为每个人获取一些信息:

1)数据2)姓名3)错误

所以对于第一个表应该是:

03.12。 10:45:00 CG System steht nichtzurVerfügung!

我正在阅读有关BS4的一些信息,但我不知道如何在python脚本下面启动。

-bash-3.2 $ cat out2.py

from bs4 import BeautifulSoup


with open ("file.txt", "r") as myfile:
    html=myfile.read().replace('\n', '')

soup = BeautifulSoup(html)
tag = soup.findAll('a') #all "a" tag in a list

count = 0
passx = 0
for i in tag:
        if count > 3:
                print "-------------------------------"
                #FILE.write("-------------------------------" + "\n")
                count = 0
                passx = 0
        if passx == 0:
                print i['href']
                #FILE.write(i['href'] + "\n")
                passx = 1
        print i.text
        count = count + 1

#FILE.close()

1 个答案:

答案 0 :(得分:0)

Find all tr代码并获取td代码by class attribute

# encoding: utf-8
from bs4 import BeautifulSoup

data = u"""
<table>
    <tr>
        <td class="zeit"><div>03.12. 10:45:00</div></td>
        <td class="system"><div><a target="_blank" href="detail.php?host=CG&factor=2&delay=1&Y=15">CG</div></a></td>
        <td class="fehlertext"><div>System steht nicht zur Verfügung!</div></td>
    </tr>

    <tr>
        <td class="zeit"><div>03.12. 10:10:01</div></td>
        <td class="system"><div><a target="_blank" href="detail.php?host=DEXProd&factor=2&delay=5&Y=15">DEX</div></a></td>
        <td class="fehlertext"><div>ssh: Connection refused Couldn't read packet: Connection reset by peer</div></td>
    </tr>

    <tr>
        <td class="zeit"><div>03.12. 06:23:06</div></td>
        <td class="system"><div><a target="_blank" href="detail.php?host=FRAUD&factor=2&delay=1&Y=15">Boni</div></a></td>
        <td class="fehlertext"><div>ID Fehler</div></td>
    </tr>
</table>
"""

soup = BeautifulSoup(data)
for tr in soup.find_all('tr'):
    zeit = tr.find('td', class_='zeit').get_text(strip=True)
    system = tr.find('td', class_='system').get_text(strip=True)
    fehlertext = tr.find('td', class_='fehlertext').get_text(strip=True)

    print zeit, system, fehlertext

打印:

03.12. 10:45:00 CG System steht nicht zur Verfügung!
03.12. 10:10:01 DEX ssh: Connection refused Couldn't read packet: Connection reset by peer
03.12. 06:23:06 Boni ID Fehler