gpt4 book ai didi

Python - 以整齐的表格格式将 HTML 抓取为 csv

转载 作者:行者123 更新时间:2023-12-01 02:11:35 27 4
gpt4 key购买 nike

我得到了下面的 python 脚本,目的是抓取 HTML 并将其以嵌套表格式保存到 csv。

enter image description here

有人可以帮忙吗?非常感谢!

最佳答案

我刚刚尝试了你的代码(稍加修改)并且它有效:

import pandas as pd

url = 'http://www.aastocks.com/en/cnhk/market/quota-balance/hk-connect'

for i, df in enumerate(pd.read_html(url)):
filename = '/tmp/output_%02d.csv' % i
df.to_csv(filename, encoding='utf-8')

输出:

daniel@synapse:/tmp$ cat output_17.csv
,0,1,2,3,4,5
0,Combined Southbound,,,,,
1,Date,Daily Quota Balance(% of Quota),Money Flow,Buy Trade Value(HKD),Sell Trade Value(HKD),Total Trade Value1 (% of Market Turnover)
2,2018/02/07,20.68B(98.46%),In 322.87M,TBA,TBA,TBA
3,2018/02/06,12.98B(61.81%),In 8.02B,25.70B,18.10B,43.80B(16.98%)
4,2018/02/05,10.76B(51.25%),In 10.24B,19.50B,8.91B,28.41B(16.72%)
5,2018/02/02,15.60B(74.29%),In 5.40B,12.43B,7.05B,19.48B(13.17%)
6,2018/02/01,18.67B(88.90%),In 2.33B,11.60B,9.89B,21.49B(13.90%)
7,2018/01/31,14.89B(70.91%),In 6.11B,14.29B,8.32B,22.61B(12.79%)
8,2018/01/30,17.55B(83.55%),In 3.45B,11.82B,8.86B,20.68B(11.92%)
9,2018/01/29,16.24B(77.35%),In 4.76B,14.98B,10.45B,25.43B(13.27%)
10,2018/01/26,17.53B(83.46%),In 3.47B,12.01B,9.20B,21.21B(11.79%)
11,2018/01/25,18.18B(86.58%),In 2.82B,13.09B,11.13B,24.22B(12.90%)
12,2018/01/24,17.02B(81.07%),In 3.98B,13.42B,10.13B,23.55B(12.54%)
13,2018/01/23,14.72B(70.07%),In 6.28B,15.35B,9.39B,24.74B(12.50%)
14,2018/01/22,14.77B(70.31%),In 6.23B,14.43B,8.21B,22.64B(13.40%)
15,2018/01/19,14.75B(70.26%),In 6.25B,13.86B,7.68B,21.54B(13.25%)
16,2018/01/18,14.78B(70.36%),In 6.22B,15.07B,8.94B,24.01B(12.23%)
17,,,,,,

仅供引用:页面字符集=UTF-8

评论更新

I use excel to open csv and find the contents is garbled.

我没有找到任何中文字符,但是如果我将URL更改为url = 'http://www.aastocks.com/sc/cnhk/market/quota-balance/hk-connect',这样中文就可以正确显示了。

import pandas as pd

url = 'http://www.aastocks.com/sc/cnhk/market/quota-balance/hk-connect'

for i, df in enumerate(pd.read_html(url, encoding='utf-8')):
filename = '/tmp/output_%02d.csv' % i
df.to_csv(filename, encoding='utf-8')

检查输出:

daniel@synapse:/tmp$ head -5 output_17.csv
,0,1,2,3,4,5
0,南向合计,,,,,
1,日期,每日额度馀额(占额度),当日资金流向,买入成交额(港元),卖出成交额(港元),总成交额1(佔大市成交%)
2,2018/02/07,218.69亿(104.14%),流出8.69亿,163.10亿,188.78亿,351.88亿(16.12%)
3,2018/02/06,129.79亿(61.81%),流入80.21亿,257.05亿,180.96亿,438.01亿(16.98%)

所以我认为 excel 中的编码有问题(如果我错了请修复我)。

关于Python - 以整齐的表格格式将 HTML 抓取为 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48654248/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com