抓取时提取html表数据的最佳方法?

问题描述

尝试从此类page中提取表数据。到目前为止编写的代码在这里:

更新

我发现使用pandasread_html可以处理html表。所以我做了

table = pd.read_html(response.css('#D_GA_Content > font > table').get())
header = dict.fromkeys(table[0].to_dict('recrod')[0].values(),'')

for data in table[0].to_dict('recrod')[1:]:
    for key,value in data.items():
        header[table[0].to_dict('record')[0][key]] += value + ' | '

问题在于表的布局正在动态更改。该代码仅适用于this one的一种表布局,但是如果您访问page1page2等这些页面,则每个表的布局都不同。下面显示了我想要的每个表的示例输出,应将其用'|'分隔在相应标题下。

为此page产生的输出

{'% of the Securities': '% of the Securities | 0.0061 | ','Date of Change': 'Date of Change | 23.10.2020 | ','Number of Shares Acquired/ (Disposed)': 'Direct | Indirect | 7500 | - | ','Remarks': 'Remarks | Acquisition of shares in the open market | ','Transaction Price Per Share (RM)': 'Transaction Price Per Share (RM) | 3.85 | '}

上方表格布局所需的输出

{'% of the Securities': '0.0061','Date of Change': ' 23.10.2020 ','Number of Shares Acquired/ (Disposed) | Indirect': '-','Number of Shares Acquired/ (Disposed) |Direct': ' 7500','Remarks': 'Acquisition of shares in the open market ','Transaction Price Per Share (RM)': ' 3.85'}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)