Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
New backend to scrape redesigned NASDAQ website
Browse files Browse the repository at this point in the history
  • Loading branch information
mmccollow committed Jun 19, 2014
1 parent 279e08a commit 465e0c5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
6 changes: 3 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from flask import Flask
from flask import render_template, url_for
from scrape import load_data, format_json
from nd_csv import get_stocks_data

app = Flask(__name__)

@app.route("/data.json")
def data_json():
return format_json(load_data())
return get_stocks_data()

@app.route("/")
def index():
Expand All @@ -19,4 +19,4 @@ def index():
return render_template("index.html", d3=d3, bootstrapjs=bootstrapjs, bootstrapcss=bootstrapcss, qr=qr)

if __name__ == "__main__":
app.run(debug=True)
app.run(debug=True)
40 changes: 40 additions & 0 deletions nd_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import csv
import urllib2
import StringIO
import json

URL = "http://www.nasdaq.com/quotes/nasdaq-100-stocks.aspx?render=download"

# try to simulate Chrome
HEADERS= {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"User-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept-Language": "en-US,en;q=0.8",
"Cookie": "clientPrefs=||||lightg; \
__atuvc=1%7C24%2C1%7C25; \
s_sess=%20s_cc%3Dtrue%3B%20s_sq%3Dnasdaqprod%253D%252526pid%25253DFlashQuotes%25252520-%25252520Nasdaq%25252520100%252526pidt%25253D1%252526oid%25253Dhttp%2525253A//www.nasdaq.com/quotes/nasdaq-100-stocks.aspx%2525253Frender%2525253Ddownload%252526ot%25253DA%3B; \
s_pers=%20bc%3D1%7C1403018677068%3B%20s_nr%3D1403105222488-Repeat%7C1410881222488%3B; \
NSC_W.TJUFEFGFOEFS.OBTEBR.80=ffffffffc3a08e3745525d5f4f58455e445a4a423660"
}

def get_stocks_data():
req = urllib2.Request(URL, headers=HEADERS)
page = urllib2.urlopen(req)

csvfile = StringIO.StringIO(page.read())
csvdict = csv.DictReader(csvfile)

output = {'children': []}
for item in csvdict:
output['children'].append({
'symbol': item['Symbol'].strip(),
'name': item[' Name'].strip(),
'price': item[' lastsale'].strip(),
'change_d': item[' netchange'].strip(),
'change_p': item['pctchange'].strip(),
'volume': item[' share_volume'].strip(),
'value': item[' Nasdaq100_points'].strip()
})
return json.dumps(output)

5 changes: 3 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ <h2>NASDAQ-100 Stock Performance</h2>
var diameter = 960,
format = d3.format(",d"),
color = d3.scale.category20c();
rSize = d3.scale.pow().domain([-100,100]).range([-50,50]);

var bubble = d3.layout.pack()
.sort(null)
.size([diameter, diameter])
.padding(1.5)
.radius(function(d) { return 20 + (1 + (d * 7)); });
.radius(function(d) { return 20 + (rSize(d) * 30); });

var svg = d3.select("#graph").append("svg")
.attr("width", diameter)
Expand Down Expand Up @@ -64,4 +65,4 @@ <h2>NASDAQ-100 Stock Performance</h2>
setInterval(drawGraph, 900000);
</script>
</body>
</html>
</html>

0 comments on commit 465e0c5

Please sign in to comment.