Reputation: 210
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:
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.
Upvotes: 0
Views: 652
Reputation: 4860
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:
Upvotes: 1