Last active
February 16, 2018 17:40
-
-
Save henriklindgren/b150259a27d734d2b1ed311d6e28f821 to your computer and use it in GitHub Desktop.
Avanza stock quotes snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# coding:utf-8 | |
from bs4 import BeautifulSoup | |
import requests | |
import sys | |
urls = [ | |
'https://www.avanza.se/aktier/om-aktien.html/5466/mycronic', | |
] | |
# the relevant fields we want to look at within the quotes block | |
change_percent_str = 'changePercent' | |
change_str = 'change' | |
buy_price_str = 'buyPrice' | |
sell_price_str = 'sellPrice' | |
last_price_str = 'lastPrice' | |
highest_price_str = 'highestPrice' | |
lowest_price_str = 'lowestPrice' | |
total_volume_traded_str = 'totalVolumeTraded' | |
timestamp_str = 'updated' | |
span_str = 'span' | |
titles = [change_percent_str, change_str, buy_price_str, sell_price_str, | |
last_price_str, highest_price_str, lowest_price_str, | |
total_volume_traded_str, timestamp_str] | |
max_title_length = len(max(titles, key=len)) | |
def format_row(columns, format_length): | |
return ' | '.join(u'{{:^{}}}'.format(format_length).format(column) for column in columns).encode('utf-8') | |
print format_row(titles, max_title_length) | |
for url in urls: | |
r = requests.get(url) | |
if r.status_code != 200: | |
print 'Avanza answered with status code ', r.status_code | |
sys.exit(1) | |
html_src = r.text | |
def get_div_content(root, html_class): | |
return root.find('div', {'class': html_class}) | |
def get_span_content(root, html_class): | |
return root.find('span', {'class': html_class}) | |
soup = BeautifulSoup(html_src, 'html.parser') | |
# Get the quotes block. | |
# The most unique identifier available to find it. | |
quotes_block_str = 'quoteWrapper' | |
quotes_block = get_div_content(soup, quotes_block_str) | |
change_percent = get_span_content(quotes_block, change_percent_str) | |
change = get_span_content(quotes_block, change_str) | |
buy_price = get_span_content(quotes_block, buy_price_str) | |
sell_price = get_span_content(quotes_block, sell_price_str) | |
last_price = get_span_content(quotes_block, last_price_str) | |
highest_price = get_span_content(quotes_block, highest_price_str) | |
lowest_price = get_span_content(quotes_block, lowest_price_str) | |
total_volume_traded = get_span_content(quotes_block, total_volume_traded_str) | |
timestamp = get_span_content(quotes_block, timestamp_str) | |
values = [change_percent.string, change.string, | |
buy_price.string, sell_price.string, | |
last_price.string, highest_price.string, | |
lowest_price.string, total_volume_traded.string, | |
timestamp.string] | |
print format_row(values, max_title_length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment