我有一个与Scrape arraytodatatable with python密切相关的问题。
我使用那里的出色技巧来处理来自网站的信息:http://www.oilsandsmagazine.com/energy-statistics/oil-and-gas-prices#dailyUSD
我阅读了该页面,并希望提取如下所示的数据部分:
var data1 = google.visualization.arrayToDataTable([
['date', 'BRENT', 'WTI', 'CDN LIGHT', 'WCS'],
[new Date(2018, 3, 1), null, null, null,null],
[new Date(2018, 3, 2), 67.64, 63.01, 58.53, 42.53],
[new Date(2018, 3, 3), 68.12, 63.51, 58.58, 44.53],
[new Date(2018, 3, 4), 68.02, 63.37, 58.12, 45.53],
[new Date(2018, 3, 5), 68.33, 63.54, 58.20, 46.05],
[new Date(2018, 3, 6), 67.11, 62.06, 56.39, 44.65],
])
我过去经常尝试提取驱动图表的数据:
pattern = re.compile(r"var data1 = google\.visualization\.arrayToDataTable\((.*?)\);", re.MULTILINE | re.DOTALL)
data = pattern.search(res.text).group(1)
data=data.replace('new Date','')
我得到的数据接近我的需求。
[
['date', 'BRENT', 'WTI', 'CDN LIGHT', 'WCS'],
[new Date(2018, 3, 1), null, null, null,null],
[new Date(2018, 3, 2), 67.64, 63.01, 58.53, 42.53],
[new Date(2018, 3, 3), 68.12, 63.51, 58.58, 44.53],
[new Date(2018, 3, 4), 68.02, 63.37, 58.12, 45.53],
[new Date(2018, 3, 5), 68.33, 63.54, 58.20, 46.05],
[new Date(2018, 3, 6), 67.11, 62.06, 56.39, 44.65],
]
但是当我尝试时:
data2 = ast.literal_eval(data)
ast.literal_eval()失败。我认为它不喜欢日期格式。我试图最终将其纳入熊猫数据框。
我之前从未使用过ast模块,因此一直在寻找可以提供的帮助/指针。
非常感谢。