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

发布于 2024-11-05 19:02:24 字数 1575 浏览 1 评论 0原文

我有一个带有表格的 html 文件(它很大,所以只给出了示例代码)。我想检索表中的值。我尝试了 python 中的 HTMLParser 库。

我开始像下面这样编码。然后我发现属性“class”与系统定义的关键字相同。所以它给了我错误。

class MyHTMLParser(HTMLParser):

    def handle_starttag(self, tag, attrs):
        if tag == 'tr':
            for class in attrs:
                if class == 'Table_row'

p = MyHTMLParser()
p.feed(ht)   

的 HTML 代码

<table class="Table_rows" cellspacing="0" rules="all" border="1" id="MyDataGrid" style="width:700px;border-collapse:collapse;">

                    <tr class="Table_Heading">

                        <td>STATION CODE</td><td>STATION NAME</td><td>SCHEDULED ARRIVAL</td><td>SCHEDULED DEPARTURE</td><td>ACTUAL/ EXPECTED ARRIVAL</td><td>ACTUAL/ EXPECTED DEPARTURE</td>

                    </tr><tr class="Table_row">

                        <td>TVC </td><td style="width:160px;">ORIGON</td><td>Starting Station </td><td>05:00, 07 May 2011</td><td>Starting Station</td><td>05:00, 07 May 2011</td>

                    </tr><tr class="alternat_table_row">

                        <td>TVP </td><td>NEY YORK</td><td>05:04, 07 May 2011</td><td>05:05, 07 May 2011</td><td>05:04, 07 May 2011</td><td>05:05, 07 May 2011</td>

</tr>               
</table>

UPDATE

如何获取标签之间的数据?

I have got a html file with table ( its a large one, so only sample code is given ). I want to retrieve the values in tables. I tried the HTMLParser library from python.

I started coding like below. Then I found that the attribute "class" is same as system defined keyword. So its giving me error.

class MyHTMLParser(HTMLParser):

    def handle_starttag(self, tag, attrs):
        if tag == 'tr':
            for class in attrs:
                if class == 'Table_row'

p = MyHTMLParser()
p.feed(ht)   

HTML code for table

<table class="Table_rows" cellspacing="0" rules="all" border="1" id="MyDataGrid" style="width:700px;border-collapse:collapse;">

                    <tr class="Table_Heading">

                        <td>STATION CODE</td><td>STATION NAME</td><td>SCHEDULED ARRIVAL</td><td>SCHEDULED DEPARTURE</td><td>ACTUAL/ EXPECTED ARRIVAL</td><td>ACTUAL/ EXPECTED DEPARTURE</td>

                    </tr><tr class="Table_row">

                        <td>TVC </td><td style="width:160px;">ORIGON</td><td>Starting Station </td><td>05:00, 07 May 2011</td><td>Starting Station</td><td>05:00, 07 May 2011</td>

                    </tr><tr class="alternat_table_row">

                        <td>TVP </td><td>NEY YORK</td><td>05:04, 07 May 2011</td><td>05:05, 07 May 2011</td><td>05:04, 07 May 2011</td><td>05:05, 07 May 2011</td>

</tr>               
</table>

UPDATE

How could I get data between the tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扭转时空 2024-11-12 19:02:24

请注意,handle_starttag 方法的文档指出:

标签参数是名称
标签转换为小写。属性
参数是(名称,值)的列表
包含找到的属性的对
在标签的 <> 内括号。

所以,您可能正在寻找类似的东西:

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'tr':
            for name, value in attrs:
                if name == 'class':
                    print 'Found class', value

p = MyHTMLParser()
p.feed(ht)   

Prints:

Found class Table_Heading
Found class Table_row
Found class alternat_table_row

PS 我还推荐 BeautifulSoup 用 Python 解析 HTML。

Note that the documentation of the handle_starttag method states:

The tag argument is the name of the
tag converted to lower case. The attrs
argument is a list of (name, value)
pairs containing the attributes found
inside the tag’s <> brackets.

So, you're probably looking for something like:

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'tr':
            for name, value in attrs:
                if name == 'class':
                    print 'Found class', value

p = MyHTMLParser()
p.feed(ht)   

Prints:

Found class Table_Heading
Found class Table_row
Found class alternat_table_row

P.S. I also recommend BeautifulSoup for parsing HTML with Python.

妄想挽回 2024-11-12 19:02:24

如何打印 STATION 等值
代码站名称起源...?。

你可以用 BeautifulSoup 来做到这一点。

from BeautifulSoup import BeautifulSoup

html = '''\
<td>STATION CODE</td><td>STATION NAME</td><td>SCHEDULED ARRIVAL</td><td>SCHEDULED DEPARTURE</td><td>ACTUAL/ EXPECTED ARRIVAL</td><td>ACTUAL/ EXPECTED DEPARTURE</td>
</tr><tr class="Table_row">
<td>TVC </td><td style="width:160px;">ORIGON</td><td>Starting Station </td><td>05:00, 07 May 2011</td><td>Starting Station</td><td>05:00, 07 May 2011</td>
'''

soup = BeautifulSoup(html)
tag = soup.findAll('td', limit=2)
tag_O = soup.findAll('td')[7]

for i in range(len(tag)):
    print tag[i].string
print tag_O.string

'''Output-->
STATION CODE
STATION NAME
ORIGON
'''

How to print the values like STATION
CODE STATION NAME ORIGON ...?.

You can do it like this with BeautifulSoup.

from BeautifulSoup import BeautifulSoup

html = '''\
<td>STATION CODE</td><td>STATION NAME</td><td>SCHEDULED ARRIVAL</td><td>SCHEDULED DEPARTURE</td><td>ACTUAL/ EXPECTED ARRIVAL</td><td>ACTUAL/ EXPECTED DEPARTURE</td>
</tr><tr class="Table_row">
<td>TVC </td><td style="width:160px;">ORIGON</td><td>Starting Station </td><td>05:00, 07 May 2011</td><td>Starting Station</td><td>05:00, 07 May 2011</td>
'''

soup = BeautifulSoup(html)
tag = soup.findAll('td', limit=2)
tag_O = soup.findAll('td')[7]

for i in range(len(tag)):
    print tag[i].string
print tag_O.string

'''Output-->
STATION CODE
STATION NAME
ORIGON
'''
秋意浓 2024-11-12 19:02:24

我强烈推荐使用 BeautifulSoup 库。它甚至可以轻松处理损坏的 HTML。

http://www.crummy.com/software/BeautifulSoup/

I would highly recommend using the BeautifulSoup library. It handles even broken HTML with ease.

http://www.crummy.com/software/BeautifulSoup/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文