Download folder from url python

I'm writing a program/script in python3. I know how to download single files from URL, but I need to download whole folder, unzip the files and merge text files.

Is it possible to download all files FROM HERE to new folder on my computer with python? I'm using a urllib to download a single files, can anyone give a example how to download whole folder from link above?

asked Sep 20, 2017 at 12:20

1

Install bs4 and requests, than you can use code like this:

import bs4
import requests

url = "//bossa.pl/pub/metastock/ofe/sesjaofe/"
r = requests.get[url]
data = bs4.BeautifulSoup[r.text, "html.parser"]
for l in data.find_all["a"]:
    r = requests.get[url + l["href"]]
    print[r.status_code]

Than you have to save the data of the request into your directory.

answered Sep 20, 2017 at 12:48

MegaIngMegaIng

6,9731 gold badge22 silver badges34 bronze badges

4

Python provides different modules like urllib, requests etc to download files from the web. I am going to use the request library of python to efficiently download files from the URLs.

Let’s start a look at step by step procedure to download files using URLs using request library−

1. Import module

import requests

2. Get the link or url

url = '//www.facebook.com/favicon.ico'
r = requests.get[url, allow_redirects=True]

3. Save the content with name.

open['facebook.ico', 'wb'].write[r.content]

save the file as facebook.ico.

Example

import requests


url = '//www.facebook.com/favicon.ico'
r = requests.get[url, allow_redirects=True]

open['facebook.ico', 'wb'].write[r.content]

Result

We can see the file is downloaded[icon] in our current working directory.

But we may need to download different kind of files like image, text, video etc from the web. So let’s first get the type of data the url is linking to−

>>> r = requests.get[url, allow_redirects=True]
>>> print[r.headers.get['content-type']]
image/png

However, there is a smarter way, which involved just fetching the headers of a url before actually downloading it. This allows us to skip downloading files which weren’t meant to be downloaded.

>>> print[is_downloadable['//www.youtube.com/watch?v=xCglV_dqFGI']]
False
>>> print[is_downloadable['//www.facebook.com/favicon.ico']]
True

To restrict the download by file size, we can get the filezie from the content-length header and then do as per our requirement.

contentLength = header.get['content-length', None]
if contentLength and contentLength > 2e8: # 200 mb approx
return False

Get filename from an URL

To get the filename, we can parse the url. Below is a sample routine which fetches the last string after backslash[/].

url= "//www.computersolution.tech/wp-content/uploads/2016/05/tutorialspoint-logo.png"
if url.find['/']:
print[url.rsplit['/', 1][1]

Above will give the filename of the url. However, there are many cases where filename information is not present in the url for example – //url.com/download. In such a case, we need to get the Content-Disposition header, which contains the filename information.

import requests
import re

def getFilename_fromCd[cd]:
"""
Get filename from content-disposition
"""
if not cd:
return None
fname = re.findall['filename=[.+]', cd]
if len[fname] == 0:
return None
return fname[0]


url = '//google.com/favicon.ico'
r = requests.get[url, allow_redirects=True]
filename = getFilename_fromCd[r.headers.get['content-disposition']]
open[filename, 'wb'].write[r.content]

The above url-parsing code in conjunction with above program will give you filename from Content-Disposition header most of the time.

Updated on 30-Jul-2019 22:30:26

  • Related Questions & Answers
  • Downloading file using SAP .NET Connector
  • How are files extracted from a tar file using Python?
  • Rename multiple files using Python
  • Using SAP Web Service from WSDL file
  • Web Scraping using Python and Scrapy?
  • Python Implementing web scraping using lxml
  • How to copy files from one folder to another using Python?
  • How to copy files from one server to another using Python?
  • How to convert PDF files to Excel files using Python?
  • How to copy certain files from one folder to another using Python?
  • Implementing web scraping using lxml in Python?
  • Does HTML5 allow you to interact with local client files from within a web browser?
  • Generate temporary files and directories using Python
  • How to remove swap files using Python?
  • How to create powerpoint files using Python

How do I download a directory in Python?

Step 1: Create a . netrc file to store your password. ... .
Step 2: List all links from a web directory. We will be using requests for data download, and parsing HTML with StringIO and etree. ... .
Step 3: Classify links into folders and data files. ... .
Step 4: Loop through subdirectories and download all new data files..

How do I download multiple files from a website using Python?

Import module. import requests..
Get the link or url. url = '//www.facebook.com/favicon.ico' r = requests.get[url, allow_redirects=True].
Save the content with name. open['facebook.ico', 'wb'].write[r.content] save the file as facebook. ... .
Get filename from an URL. To get the filename, we can parse the url..

How do I download a file from a specific directory in Python?

Download a file to a custom folder: To download a file to a specific folder, pass it the --directory-prefix or -P flag, followed by the destination folder.

How do I automatically download a file from a website using Python?

“how to automate downloading a file from a website using python” Code Answer's.
import urllib. request..
pdf_path = "".
def download_file[download_url, filename]:.
response = urllib. request. urlopen[download_url].
file = open[filename + ".pdf", 'wb'].
file. write[response. read[]].
file. close[].

Chủ Đề