Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting data from interactive line chart - svg path - python 2.7

I would like to get the data depicted on the sentiment value line chart: http://sentdex.com/financial-analysis/?i=TWTR&tf=7d

Looking for answers I went through Web scraping data from an interactive chart that seems to be very similar to my case.

Also went through: Scraping graph data from a website using Python

This is my last attempt:

import re

svg_string = "M 364.5 53 L 364.5 171.35000000000002 M 364.5 184.5 L 364.5 302.85 M 364.5 184.5 L 364.5 302.85"

print repr(svg_string)

data = [map(float, xy.split(',')) for xy in re.split('[ML]', svg_string)[1:]]
print data

I am facing at least 3 issues: The first one is that the data for svg_string represents coordinates vs. real values so I am not sure how to access the interesting data.

The second is that even when I play with this code I am getting

 ValueError: invalid literal for float(): 364.5 53

And last, the string for svg_string does not even represent the graph properly (I cannot find the right code).

How do I extract the values? Thank you in advance.

like image 582
Diego Avatar asked Jul 20 '26 12:07

Diego


1 Answers

It's hard to know exactly what you're after overall, but the ValueError you are getting is because your data is not exactly the same as the other question you referenced. You have spaces in your data where the other question had commas.

To alleviate the ValueError change:

data = [map(float, xy.split(',')) for xy in re.split('[ML]', svg_string)[1:]]

to:

data = [map(float, xy.split()) for xy in re.split('[ML]', svg_string)[1:]]

Hopefully this gets you onto the next step.

Edit:

Ok so I looked at the page again, and the data is literally just in a js variable that you can grab from the response. The variable name is 'series' so you either need to do some parsing yourself to grab the data or find a library to work with (e.g. BeautifulSoup, etc.).

like image 159
craigts Avatar answered Jul 22 '26 00:07

craigts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!