Guest User

ICScards statement export script

a guest
Sep 13th, 2015
164
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. __author__ = 'Martin Gill'
  4.  
  5. from bs4 import BeautifulSoup
  6. import requests
  7. import csv
  8. import re
  9.  
  10. url = "https://www.icscards.nl/abnamrogb/mijn"
  11. loginUrl = "https://www.icscards.nl/pkmslogin.form"
  12.  
  13. payload = {'username': 'username', 'password': 'password', 'login-form-type': 'pwd'}
  14. s = requests.Session()
  15. r = s.post(loginUrl, payload, verify=False)
  16. data = r.content
  17. soup = BeautifulSoup(data)
  18. print(soup.prettify())
  19.  
  20. r = s.get(url)
  21. data = r.content
  22. soup = BeautifulSoup(data)
  23.  
  24. table = soup.select("table.statement-table")[0]
  25. headers = table.select("th")
  26.  
  27. outputRows = []
  28.  
  29. rows = table.select("tr.transaction-row.statement-current")
  30.  
  31. for row in rows:
  32.     cols = row.select("td")
  33.     rowItems = []
  34.     for col in cols:
  35.         # Replace newlines with whitespace
  36.         text = col.getText().replace("\n", " ").strip()
  37.         rowItems.append(text)
  38.  
  39.     # skips the "header" row
  40.     if len(rowItems) > 0:
  41.         # Remove commas
  42.         rowItems[5] = rowItems[5].replace(',', '')
  43.         # Eliminate currency symbol and other crap
  44.         rowItems[5] = re.sub(r"^.*?([0-9.]+).*?$", r"\1", rowItems[5])
  45.         # Credit/Debit handling
  46.         if re.match(r"Debit", rowItems[4]):
  47.             rowItems[5] = '-' + rowItems[5]
  48.         print(rowItems);
  49.         outputRows.append(rowItems)
  50.  
  51. with open('test.csv', 'w') as csvfile:
  52.     writer = csv.writer(csvfile,
  53.                         delimiter=',',
  54.                         quotechar='"',
  55.                         quoting=csv.QUOTE_MINIMAL)
  56.     writer.writerows(outputRows)
RAW Paste Data