0

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: https://bank.gov.ua/en/markets/exchangerate-chart?startDate=2022-01-01&endDate=2022-07-11 and if you look at the bottom there is an option to download the data in csv format.

This is my code so far:

import requests

response = requests.get(url)
data = response.content

If we call the data variable we get a byte type format of the webpage, and I just want to get the section of data illustrated in the picture which I guess has the heading of "data" followed by what seems a dictionary:

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.

2
  • It says Error 16 on the site. Commented Jul 11, 2022 at 11:15
  • what do you mean? Commented Jul 11, 2022 at 11:24

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.