Reputation: 1217
I have almost the question already posted and answered here:
Perform Download via download button in Python
I also followed the instructions, in the answer of the above link.
In my case I want to download the data from the following page:
The download-button is the one called "Datei herunterladen" in the lower right corner.
I went into the inspect mode downloaded the file and got the following output in the inspection
But the resulting
Request URL: 'https://www.smard.de/nip-download-manager/nip/download/market-data'
does not help getting the csv-file. Opened in the browser I get: 'The requested URL was rejected.' On the other hand, it does not even contain the parameters anymore, so it can't be the right download url.
May anyone help to automate this download?
edit Now I also tried
url = 'https://www.smard.de/nip-download-manager/nip/download/market-data'
json_body = {'format': "CSV",
'language': "de",
'moduleIds': [1001224, 1004066, 1004067, 1004068, 1001223, 1004069, 1004071, 1004070, 1001226, 1001228, 1001227,1001225],
'region': "DE",
'timestamp_from': 1659304800000,
'timestamp_to': 1659391199999,
'type': "discrete"}
x = requests.post(url, json = json_body)
x.content
> b'Exception when parsing incoming request to JSON object.'
So how do I get the csv-file based on this method?
Upvotes: 1
Views: 676
Reputation: 1217
The final solution is:
url = 'https://www.smard.de/nip-download-manager/nip/download/market-data'
payload = {"request_form":[{"format":"CSV","moduleIds":[1001224,1004066,1004067,1004068,1001223,1004069,1004071,1004070,1001226,1001228,1001227,1001225],"region":"DE","timestamp_from":1658872800000,"timestamp_to":1659563999999,"type":"discrete","language":"de"}]}
x = requests.post(url, json = payload)
with open(f'energy_ger.csv', 'wb') as f:
f.write(x.content)
Upvotes: 0
Reputation: 160
the reason that you're getting "URL Rejected" is because the link specified in the request is not file-specific. The link that the POST request is being made to, contains several files, which are given out based on the body that the request is sent with. If you look into the developer tools, you can see that there is a request payload attached to the request (image attached) . If you want to learn more about sending JSON body data with requests, please take a look at w3 and this thread. I hope that helps!
Upvotes: 1