Home > Net >  Get CSV data from URL - Python
Get CSV data from URL - Python

Time:07-11

I am trying to export some csv data and I want to do it preferable by reading the data using the url and then exporting.

The url I want to get data from is: enter image description here

I am not really sure if there is a way of cleaning it all up or a way of indexing but I could really use some help.

CodePudding user response:

The string you are after starts after window.exchangeRate = JSON.parse(', and ends at ');, so slice the response text (which is of type str) at those indexes, then use json.loads to transform the string to a dict.

import requests
import json

url = r"https://bank.gov.ua/en/markets/exchangerate-chart?startDate=2022-01-01&endDate=2022-07-11"

response = requests.get(url)
text = response.text

left_str = r"window.exchangeRate = JSON.parse('"
right_str = r"');"

left = text.find(left_str)   len(left_str)
right = text.find(right_str, left)

data = json.loads(text[left:right])

See:

  • Related