'Download a zip file from a DL link to a specific folder in Python

I have this download link that I've extracted from a site with the following code:

import urllib
from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
import re

url = r'https://rawkuma.com/manga/kimetsu-no-yaiba/'
req = Request(url, headers={'User-Agent':'Chrome'})
html = urlopen(req)
soup = BeautifulSoup(html, "html.parser")
body = soup.find_all('body')
content = body[0].find_all('div',{'id':'content'})
ul = content[0].find_all('ul')
chapter = ul[0].find_all('li',{'data-num':'128'})
dt = chapter[0].find_all('div',{'class':'dt'})
a = dt[0].find_all('a')
a = str(a[0])
href = a.split(" ")[2][5:]

Now I want to use that href which is a DL link to a zip file and download it to a specified folder. I've tried something like this:

save_path = r'C:\Users\...'
file_name = r'kimetsu-no-yaiba-chapter-128'
completeName = os.path.join(save_path, file_name+".zip")
file1 = open(completeName, "w")
file1.write(href)
file1.close()

But this seems to just add an empty zip file to the folder. And if I try to open the url first before putting into the write function it gives me an error:

req = Request(href)
r = urlopen(req) 

save_path = r'C:\Users\...'
file_name = r'kimetsu-no-yaiba-chapter-128'
completeName = os.path.join(save_path, file_name+".zip")
file1 = open(completeName, "w")
file1.write(r)
file1.close()

But I get this error:

urllib.error.URLError: <urlopen error unknown url type: "https>


Solution 1:[1]

The url http://dl.rawkuma.com/?id=86046 is not the actual URI with the zip file, there is a redirect to the real link. So here is the code to download a zip file based on your example. You need to install the requests library to make it easier.

import requests
import os

URL = 'http://dl.rawkuma.com/?id=86046'

res = requests.get(URL, allow_redirects=True)

# this is the actual url for the zip file
print(res.url)

with requests.get(res.url, stream=True) as r:
        r.raise_for_status()
        print('downloading')
        with open(os.path.join('.', 'file.zip'), 'wb') as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zhong Dai