r/ScriptSwap • • Nov 29 '14

[REQUEST] Parse HTML from company financial statements

Hello,

I am wondering if someone could help me develop a script to parse data from an HTML table like the ones here (see item 8, the income statement). Basically what I'm trying to do is pull that data (net income, revenue, various expenses etc) into a google spreadsheet so that I can run some calculations on it.

I wrote a similar script in Python a while ago that scraped data from Yahoo Finance's financial statements but it is dependent on Yahoo's formatting and I would trust that the default formatting from Edgar is much more likely to remain the same than Yahoo's, though Yahoo hasn't changed theirs in a while either.

Anyway just looking for some guidance or advice if anyone has a better way to do this. Basically I have a series of checks I want to perform against revenues, profit margins, various ratios, etc and it would be much easier to run those checks if I could use the raw financial data in my spreadsheet. Most of the APIs I have found (Yahoo Finance, Morningstar, Google Finance so far) only provide quote data like bid, ask, close, open, % change, and basic fundamental data like PE ratio and the like. Also they don't provide any access to historical data unless it is quote data, so I can't determine any trends in fundamental data.

Ok, hopefully that's enough info. Any advice? Thanks!!!

Chris

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/greenbartrading Nov 29 '14

Thanks for the response. I set up Python on my Windows machine and grabbed those old scripts as a starting point but I keep getting a list index out of range error during the variable creation. IF you get a few minutes and feel like installing python and trying to get it to work that would be cool. I will keep working on it in the mean time. I did find one bug in the code where I had to convert this:

data = str(row.getText(separator=' '))

to this:

data = (row.getText(separator=' ')).encode('utf-8')

and now I am through to the point where I'm getting the index error. So if you install/run this you might have to change that also. I'm using Python 2.7.8 with BeautifulSoup 4.3 (I think...it's whatever the latest one is cause I just downloaded it) and mechanize. So if you install python 2.7.8 and install the mechanize and bs4 modules it should work. I just can't wrap my head around how I built these variables yet as it was along time ago, lol. I know that I am pulling from the URLs in the code and building an individual variable for each "cell" on the table on the website, and they are named according to the initials (like balance sheet, cash cash and cash equivalents, yr1 is called bscceyr1 and so on).

Let me know if you dig in and can figure out this error as I think once I figure this out I can just modify the variables and build my new calculations! Oh btw remember you need both .py files as the yscrape file calls calculate.py in order to run the calcs once it builds the variables.

Thanks!!

1

u/worksafeforposterity Nov 30 '14

what variable is getting 'index out of range' error? if it's your row-grabbing logic, it's probably grabbing the wrong table & no further rows exist. does the table you're interested in have an 'id' or a 'class' attribute? (you can check by right-clicking the table & 'inspect element'ing)

1

u/greenbartrading Nov 30 '14

I figured it out. It was not putting any items in the array because I was stripping the array of non-digits and there were some random ASCII bytes on the end of the numbers. I encoded with utf-8 and stripped the extra characters and now it works. Started adding in some new calculations and am off and running! Thanks!!!

1

u/greenbartrading Nov 30 '14

And also yeah I grabbed the table by class now so hopefully I won't need to change it much if at all.