【问题标题】:Expressive python library for parsing HTML tables用于解析 HTML 表格的富有表现力的 Python 库
【发布时间】:2012-05-07 06:54:58
【问题描述】:

我需要解析 html 表格以执行诸如获取某个单元格上方/下方或左/右列中的所有单元格之类的操作。有没有可以轻松做到这一点的python库?

【问题讨论】:

    标签: python html-parsing html-table


    【解决方案1】:

    此代码将页面中的所有表格转换为列表。

    import pandas as pd
    url = r'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
    tables = pd.read_html(url) # Returns list of all tables on page
    sp500_table = tables[0] # Select table of interest
    

    【讨论】:

      【解决方案2】:

      看看pyquery。它允许对 xml 文档进行 jquery 查询。快速查看API 似乎 prevAll 和 nextAll 可以找到左/右单元格。想得到上面/下面的也不会那么难。

      【讨论】:

        【解决方案3】:

        您可以使用lxml - XML 和 HTML 与 Python - 来解析表格。 Here 是一个简单的示例,说明您可以对表执行哪些操作(加载和遍历行)。

        【讨论】:

          【解决方案4】:

          【讨论】:

            猜你喜欢
            • 2012-07-03
            • 2015-02-01
            • 2020-03-14
            • 2014-12-16
            • 2017-03-03
            • 1970-01-01
            • 2013-08-03
            • 2014-09-23
            • 1970-01-01
            相关资源
            最近更新 更多