'Issues with downloading file from website using python request
I have the following code:
import requests
r = requests.get('https://etf.invesco.com/de/private/de/product/invesco-kbw-nasdaq-fintech-ucits-etf-acc/reports/IndexConstituentsEquity.xlsx', allow_redirects=True)
open('test.xlsx', 'wb').write(r.content)
I try to download the file, but it doesn't work. I guess the problem is that on the website a so called country splash comes up, but unfortunately I don't know how to deal with that.
I solved the problem by using Selenium, but this is not the way actually I would like to go as I would like to use the requests package:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PATH = "D:\Python\chromedriver.exe"
driver = webdriver.Chrome(executable_path=PATH)
investortype = 'body > div:nth-child(2) > main:nth-child(3) > div:nth-child(2) > div:nth-child(2) > form:nth-child(7) > div:nth-child(8) > div:nth-child(1) > div:nth-child(4) > div:nth-child(1) > label:nth-child(2)'
submit_CSS_Selector = "button[class='o-button']"
driver.maximize_window()
driver.get('https://etf.invesco.com/de/private/de/product/')
# investortype & submit
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, investortype))).click()
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, submit_CSS_Selector))).click()
time.sleep(2)
driver.get('https://etf.invesco.com/de/private/de/product/invesco-kbw-nasdaq-fintech-ucits-etf-acc/reports/IndexConstituentsEquity.xlsx')
I would very much appreciate if anyone one would be so kind and could help me in solving the problem using the request package instead of Selenium. Thank you in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|