downloading a file using python without knowing the file type (file name + file type)

I have been trying to download a file from the American Gut Project in qiita (study number 10317).
I can download directly from the web, however I want to create a python code to download the files automatically. This is the url from the site I need to download from: https://qiita.ucsd.edu/study/description/10317
and this is the data I need to download:
https://qiita.ucsd.edu/download_raw_data/10317.
As you might see, the ending of the file I want to download is not a <path/to/website/file_name.file_type>.

because the download url is not as specified above, I couldn’t use the requests library to download it
`req = requests.get(download_url)
filename = req.url[downloadurl.rfind(‘/’)+1:]

with open(filename, ‘wb’) as f:
for chunk in req.iter_content(chunk):
if chunk:
f.write(chunk)`

correction and clarification:
The problem here is that when I try to download the file I get an html file describing the content of the download link.
One of the issues that came to light thanks to the user chepner, is that when trying to use the link it redirects the user to the https://qiita.ucsd website. Is there a way to bypass that? Is there a solution in order to download the file in python?

Thanks in advance!

3

I understand the issue you’re facing. To automate the download using Python despite the redirection to the authentication page, you can use Selenium, a tool for automating web browsers. Below is a Python code snippet that demonstrates how you can achieve this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Set up Chrome options to specify the download directory and enable automatic download
download_dir = "/path/to/download/folder" # Replace with your desired download directory
options = webdriver.ChromeOptions()
prefs = {
"download.default_directory": download_dir,
"download.prompt_for_download": False, # Disable download prompt
"download.directory_upgrade": True, # Ensure directory is updated if changed
"safebrowsing.enabled": True # Enable safe browsing to avoid blocking
}
options.add_experimental_option("prefs", prefs)
# Initialize the WebDriver with the specified options
driver = webdriver.Chrome(executable_path='/path/to/chromedriver', options=options)
try:
# Step 1: Go to the login page
driver.get('https://qiita.ucsd.edu/')
# Step 2: Locate and fill in the login form
username = driver.find_element(By.ID, 'login_field') # Replace with actual ID of the username field
password = driver.find_element(By.ID, 'password') # Replace with actual ID of the password field
username.send_keys('your_username') # Replace with your username
password.send_keys('your_password') # Replace with your password
password.send_keys(Keys.RETURN) # Submit the form
# Step 3: Wait for the welcome phrase to appear in the navigation bar
wait = WebDriverWait(driver, 10)
welcome_phrase = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="navigation-bar"]/div/div[2]/ul[2]/li[1]/a')))
# Step 4: Go to the download page
driver.get('https://qiita.ucsd.edu/download_raw_data/10317')
# The download should start automatically.
time.sleep(10) # Wait for the download to complete
finally:
# Close the driver
driver.quit()
</code>
<code>from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # Set up Chrome options to specify the download directory and enable automatic download download_dir = "/path/to/download/folder" # Replace with your desired download directory options = webdriver.ChromeOptions() prefs = { "download.default_directory": download_dir, "download.prompt_for_download": False, # Disable download prompt "download.directory_upgrade": True, # Ensure directory is updated if changed "safebrowsing.enabled": True # Enable safe browsing to avoid blocking } options.add_experimental_option("prefs", prefs) # Initialize the WebDriver with the specified options driver = webdriver.Chrome(executable_path='/path/to/chromedriver', options=options) try: # Step 1: Go to the login page driver.get('https://qiita.ucsd.edu/') # Step 2: Locate and fill in the login form username = driver.find_element(By.ID, 'login_field') # Replace with actual ID of the username field password = driver.find_element(By.ID, 'password') # Replace with actual ID of the password field username.send_keys('your_username') # Replace with your username password.send_keys('your_password') # Replace with your password password.send_keys(Keys.RETURN) # Submit the form # Step 3: Wait for the welcome phrase to appear in the navigation bar wait = WebDriverWait(driver, 10) welcome_phrase = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="navigation-bar"]/div/div[2]/ul[2]/li[1]/a'))) # Step 4: Go to the download page driver.get('https://qiita.ucsd.edu/download_raw_data/10317') # The download should start automatically. time.sleep(10) # Wait for the download to complete finally: # Close the driver driver.quit() </code>
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# Set up Chrome options to specify the download directory and enable automatic download
download_dir = "/path/to/download/folder"  # Replace with your desired download directory

options = webdriver.ChromeOptions()
prefs = {
    "download.default_directory": download_dir,
    "download.prompt_for_download": False,  # Disable download prompt
    "download.directory_upgrade": True,     # Ensure directory is updated if changed
    "safebrowsing.enabled": True            # Enable safe browsing to avoid blocking
}
options.add_experimental_option("prefs", prefs)

# Initialize the WebDriver with the specified options
driver = webdriver.Chrome(executable_path='/path/to/chromedriver', options=options)

try:
    # Step 1: Go to the login page
    driver.get('https://qiita.ucsd.edu/')

    # Step 2: Locate and fill in the login form
    username = driver.find_element(By.ID, 'login_field')  # Replace with actual ID of the username field
    password = driver.find_element(By.ID, 'password')     # Replace with actual ID of the password field

    username.send_keys('your_username')  # Replace with your username
    password.send_keys('your_password')  # Replace with your password
    password.send_keys(Keys.RETURN)      # Submit the form

    # Step 3: Wait for the welcome phrase to appear in the navigation bar
    wait = WebDriverWait(driver, 10)
    welcome_phrase = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="navigation-bar"]/div/div[2]/ul[2]/li[1]/a')))

    # Step 4: Go to the download page
    driver.get('https://qiita.ucsd.edu/download_raw_data/10317')

    # The download should start automatically.
    time.sleep(10)  # Wait for the download to complete

finally:
    # Close the driver
    driver.quit()

You can change the default directory where you want to save this file automatically :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>download_dir = "/path/to/download/folder"
</code>
<code>download_dir = "/path/to/download/folder" </code>
download_dir = "/path/to/download/folder" 

Make sure to replace 'path_to_chromedriver_executable' with the actual path to your ChromeDriver executable. And if you want to use headless browser, you can add it as option on the driver options we have defined.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật