Downloading a spreadsheet from a link in Python

Viewed 41

I am trying to download a spreadsheet from this website https://www.theice.com/clear-singapore/risk-management#margin-rates. The file I want to download is called Margin Scanning (at the bottom of the website). Normally I would click on the link "Margin Scanning" to download but I want to use Python to automate it. Then I want the context to become my dataframe so that I can amend it and save it on my drive. Any idea how I can get it done? I know how to save a table from the web but I am not sure how to download a file from the link.

This is what I have tried:

import requests
url="https://www.theice.com/publicdocs/clear_singapore/irmParameters/ICSG_MARGIN_SCANNING_20200702.CSV"
response = requests.get(url)
margin_scanning = pd.DataFrame()
margin_scanning = response.content
1 Answers

Working code: pandas Version: 1.0.3

import pandas as pd
import requests,csv
url = "https://www.theice.com/publicdocs/clear_singapore/irmParameters/ICSG_MARGIN_SCANNING_20200702.CSV"
response = requests.get(url)
response=response.content.decode('utf-8')
cr = csv.reader(response.splitlines(), delimiter=',')
data=pd.DataFrame(cr, index=None)
print(data.head())

Output:

               0              1   ...                   29              30
0  Effective Date  Exchange Code  ...  Position Allocation  Margin Erosion
1       02-JUL-20              G  ...                   No              No
2       02-JUL-20              G  ...                   No              No
3       02-JUL-20              G  ...                   No              No
4       02-JUL-20              G  ...                   No              No

[5 rows x 31 columns]
Related