我主要是这个平台上的潜伏者,并尝试使用已提出的问题的答案来解决我的问题,但我找不到当前问题的问题。 我尝试使用 scrapy 从这个website网站上抓取数据。我已经能够抓取我需要的大部分数据,但是,我想从两个交互式高图表中获取数据。
到目前为止我尝试过的:
要查看图表,您必须登录
此处。
我创建了一个一次性帐户:
电子邮件:[email protected]
,密码:
12345
,这样您就可以看到数据。
更新:scarpy_splash
,它允许使用 lua 执行 javascript 代码。通过下面的代码,我可以抓取我需要的所有数据。
LUA_SCRIPT = """
function main(splash)
-- Get cookies from previous session
splash:init_cookies(splash.args.cookies)
assert(splash:go(splash.args.url))
assert(splash:wait(0.5))
-- Extract data from page
-- Read amount of variables in second table
table_2_no_series = splash:evaljs('Highcharts.charts[1].series.length')
-- If second table has more variable then one, get this data aswell
if (table_2_no_series==2) or (table_2_no_series==3) then
table_2_y1_data = splash:evaljs('Highcharts.charts[1].series[0].yData')
table_2_y1_name = splash:evaljs('Highcharts.charts[1].series[0].name')
end
if (table_2_no_series==3) then
table_2_y3_data = splash:evaljs('Highcharts.charts[1].series[2].yData')
table_2_y3_name = splash:evaljs('Highcharts.charts[1].series[2].name')
end
return {
-- Extract webiste title
title = splash:evaljs('document.title'),
-- Extract first table data
table_1_name = splash:evaljs('Highcharts.charts[0].title.textStr'),
-- Extract Timestamps
table_1_x = splash:evaljs('Highcharts.charts[0].series[0].xAxis.categories'),
-- Extract Finanzierungsstand
table_1_y_data = splash:evaljs('Highcharts.charts[0].series[1].yData'),
table_1_y_name = splash:evaljs('Highcharts.charts[0].title.textStr'),
-- Extract second table data
table_2_y1_data,
table_2_y1_name,
table_2_y3_data,
table_2_y3_name,
cookies = splash:get_cookies(),
}
end
"""
SCRAPY_ARGS = {
'lua_source': LUA_SCRIPT,
'cookies' : self.cookies
}
# Look for json data if we sucessfully logged in
yield SplashRequest(url=response.url,
callback=self.parse_highchart_data,
endpoint='execute', args=SCRAPY_ARGS,
session_id="foo")
注意:highchart api 还有一个 .getCSV
,可以以 csv 格式导出数据。不过这个网站好像屏蔽了这个功能。
console.log(Highcharts.charts)
,显示页面上渲染的图表数组。接下来,转到特定图表 -> 系列 -> 数据,例如:
console.log(Highcharts.charts[0].series[1].data)