使用Python请求从网站下载文件的问题
我有以下代码:
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)
我尝试下载该文件,但它不起作用。我想问题是在网站上出现了一个所谓的乡村飞溅,但不幸的是我不知道该如何处理。
我通过使用硒解决了问题,但这并不是我想使用请求包的方法:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论