EDIT:
So I have save the script codes below to a text file but using re to extract the data still doesn't return me anything. My code is:
file_object = open('source_test_script.txt', mode="r")
soup = BeautifulSoup(file_object, "html.parser")
pattern = re.compile(r"^var (chart[0-9]+) = new Highcharts.Chart\(({.*?})\);$", re.MULTILINE | re.DOTALL)
scripts = soup.find("script", text=pattern)
profile_text = pattern.search(scripts.text).group(1)
profile = json.loads(profile_text)
print profile["data"], profile["categories"]
I would like to extract the chart's data from a website. The following is the source code of the chart.
<script type="text/javascript">
jQuery(function() {
var chart1 = new Highcharts.Chart({
chart: {
renderTo: 'chart1',
defaultSeriesType: 'column',
borderWidth: 2
},
title: {
text: 'Productions'
},
legend: {
enabled: false
},
xAxis: [{
categories: [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016],
}],
yAxis: {
min: 0,
title: {
text: 'Productions'
}
},
series: [{
name: 'Productions',
data: [1,1,0,1,6,4,9,15,15,19,24,18,53,42,54,53,61,36]
}]
});
});
</script>
There are several charts like that from the website, called "chart1", "chart2", etc. I would like to extract the following data: the categories line and the data line, for each chart:
categories: [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016]
data: [1,1,0,1,6,4,9,15,15,19,24,18,53,42,54,53,61,36]