【问题标题】:Using BeautifulSoup to Parse Table使用 BeautifulSoup 解析表
【发布时间】:2017-02-18 03:15:38
【问题描述】:

我正在尝试使用 BeautifulSoup 从 HTML 表中提取数据并将其转换为 n x 7 数据框,其中包含以下列:日期、交易、清单编号、发货日期、付款类型、金额和预付余额。

到目前为止我的代码片段:

def find_account_status(htmls):
soup = BeautifulSoup(htmls)
table = soup.find('table', border="0", cellpadding="2")
table2 = table.find_all("td", {"class": "bodytext"}, text=True)
print(table2.text.split())

这是我试图提取的 HTML 的 sn-p:

【问题讨论】:

    标签: python web-scraping beautifulsoup html-table


    【解决方案1】:

    你可以使用pandas.read_html():

    import pandas as pd
    
    soup = BeautifulSoup(htmls)
    table = soup.find('table', border="0", cellpadding="2")
    df = pd.read_html(str(table))[0]
    

    【讨论】:

    • 尝试从 Pycharm 下载 lxml 但我收到以下错误:错误:b"'xslt-config' 不是内部或外部命令,\r\noperable program or batch file。\r \n"
    • 因为当我运行你的代码时,我得到错误“lxml not found, please install it”
    • @RileyHun 好吧,有很多关于安装 lxml 的问题和解决方案的信息,只需 google 即可。或者,您可以通过提供 flavor 参数 (docs) 来更改解析器。
    猜你喜欢
    • 2017-02-15
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多