Opdatering 13. oktober 2021: Dette program fungerer ikke længere. Brug den opdaterede udgave i stedet.
Her er en opdatering af mit gamle program til at hente transaktioner ud fra Nordnet. Det er opdateret til at fungere med Nordnets nye design og API:
# -*- coding: utf-8 -*-
# Author: Morten Helmstedt. E-mail: helmstedt@gmail.com
""" This program logs into a Nordnet account and extracts transactions as a csv file.
Handy for exporting to Excel with as few manual steps as possible """
import requests
from datetime import datetime
from datetime import date
# USER ACCOUNT, PORTFOLIO AND PERIOD DATA. SHOULD BE EDITED FOR YOUR NEEDS #
# Nordnet user account credentials and accounts/portfolios names (choose yourself) and numbers.
# To get account numbers go to https://www.nordnet.dk/transaktioner and change
# between accounts. The number after "accid=" in the new URL is your account number.
# If you have only one account, your account number is 1.
user = ''
password = ''
accounts = {
"Frie midler: Nordnet": "1",
"Ratepension": "3",
}
# Start date (start of period for transactions) and date today used for extraction of transactions
startdate = '2013-01-01'
today = date.today()
enddate = datetime.strftime(today, '%Y-%m-%d')
# Manual data lines. These can be used if you have portfolios elsewhere that you would
# like to add manually to the data set. If no manual data the variable manualdataexists
# should be set to False
manualdataexists = True
manualdata = """
Id;Bogføringsdag;Handelsdag;Valørdag;Transaktionstype;Værdipapirer;Instrumenttyp;ISIN;Antal;Kurs;Rente;Afgifter;Beløb;Valuta;Indkøbsværdi;Resultat;Totalt antal;Saldo;Vekslingskurs;Transaktionstekst;Makuleringsdato;Verifikations-/Notanummer;Depot
;30-09-2013;30-09-2013;30-09-2013;KØBT;Obligationer 3,5%;Obligationer;;72000;;;;-69.891,54;DKK;;;;;;;;;;Frie midler: Finansbanken
"""
# CREATE VARIABLES FOR LATER USE. #
# Creates a dictionary to use with cookies
cookies = {}
# A variable to store transactions before saving to csv
transactions = ""
# LOGIN TO NORDNET #
# First part of cookie setting prior to login
url = 'https://classic.nordnet.dk/mux/login/start.html?cmpi=start-loggain&state=signin'
request = requests.get(url)
cookies['LOL'] = request.cookies['LOL']
cookies['TUX-COOKIE'] = request.cookies['TUX-COOKIE']
# Second part of cookie setting prior to login
url = 'https://classic.nordnet.dk/api/2/login/anonymous'
request = requests.post(url)
cookies['NOW'] = request.cookies['NOW']
# Actual login that gets us cookies required for later use
url = 'https://classic.nordnet.dk/api/2/authentication/basic/login'
request = requests.post(url,cookies=cookies, data = {'username': user, 'password': password})
cookies['NOW'] = request.cookies['NOW']
cookies['xsrf'] = request.cookies['xsrf']
# Getting a NEXT cookie
url = 'https://classic.nordnet.dk/oauth2/authorize?client_id=NEXT&response_type=code&redirect_uri=https://www.nordnet.dk/oauth2/'
request = requests.get(url, cookies=cookies)
cookies['NEXT'] = request.history[1].cookies['NEXT']
# GET TRANSACTION DATA #
# Payload and url for transaction requests
payload = {
'locale': 'da-DK',
'from': startdate,
'to': enddate,
}
url = "https://www.nordnet.dk/mediaapi/transaction/csv/filtered"
firstaccount = True
for portfolioname, id in accounts.items():
payload['account_id'] = id
data = requests.get(url, params=payload, cookies=cookies)
result = data.content.decode('utf-16')
result = result.replace('\t',';')
result = result.splitlines()
firstline = True
for line in result:
# For first account and first line, we use headers and add an additional column
if line and firstline == True and firstaccount == True:
transactions += line + ';' + "Depot" + "\n"
firstaccount = False
firstline = False
# First lines of additional accounts are discarded
elif line and firstline == True and firstaccount == False:
firstline = False
# Content lines are added
elif line and firstline == False:
# Fix because Nordnet sometimes adds one empty column too many
if line.count(';') == 23:
line = line.replace('; ',' ')
transactions += line + ';' + portfolioname + "\n"
# ADD MANUAL LINES IF ANY #
if manualdataexists == True:
manualdata = manualdata.split("\n",2)[2]
transactions += manualdata
# Saves CSV
with open("transactions.csv", "w", encoding='utf8') as fout:
fout.write(transactions)
Opdatering 13. oktober 2021: Dette program fungerer ikke længere. Brug den opdaterede udgave i stedet.