简体   繁体   中英

python download files too slow

python give me 2.21s/MB which isn't the real speed of my internet, i don't know why..

this is the code i try to download without tqdm but that didn't work i try to download without stream = true and try with smaller file and it's donwloading with good speed but some time it's stop downloading

import requests
from bs4 import BeautifulSoup
from tqdm import tqdm 
links = []



def download_from_anon(url):
    global link, total_size, vid_link
    req = requests.get(url)
    soup = BeautifulSoup(req.text, "html.parser")
    div = soup.find("div", { "id": "download-wrapper" })
    link = div.findAll("a")[0]['href']
    vid_link = requests.get(link, stream = True)
    total_size = int(vid_link.headers['content-length'])

def get_the_links_from_gateanime(url):
    global LINK4SH
    tst = requests.get(url, headers=headers)

    soup = BeautifulSoup(tst.content, "html.parser")
    tbody = soup.find('tbody')
    tr = tbody.findAll('tr')

    for i in tr:
        td = i.findAll('td')
        span = td[2].find("span")
        img = span.findAll("img")[0]['alt']

        quality = td[4].find("span").text  
        if "4shared" in img:
            get_the_link(td[1], quality)
        elif " Anonfiles" in img:
            get_the_link(td[1], quality)
    print(links)
    
def get_the_link(tdList, qu):
    if qu == "1080p" or qu == "720p":
        LINK = tdList.find('a')['href']
        links.append(LINK)
    
    if '4shared' in links[0] and '1080FHD' in links[0]:
        links.pop(0)

def main():
    #for i in range(180, 221):

    chunk_size = 1024*1024
    
    url = "https://ww.gateanime.com/movie/%d9%81%d9%8a%d9%84%d9%85-kimetsu-no-yaiba-movie-mugen-ressha-hen-%d9%85%d8%aa%d8%b1%d8%ac%d9%85/"


    get_the_links_from_gateanime(url)
    download_from_anon(links[0])
    with open(f"D:\igxs\spend time folder\\Kimetsu no Yaiba Movie: Mugen Ressha-hen.mp4", 'wb') as f:
        for data in tqdm(iterable = vid_link.iter_content(chunk_size = chunk_size), total = int(total_size/chunk_size), unit = 'MB'):
            f.write(data)
#-----------FUNCTION SECTION END-----------#

#-----------------------------------------------------#

#-----------WORK SECTION START-----------#

main()

i know it's really messy but that's it:)

You can download a file as urrlib module as below:

Firstly import urllib module

import urllib.requests

Then use necessary method for download:

def download_from_anon(url):
    
    global link, total_size, vid_link
    req = requests.get(url)
    soup = BeautifulSoup(req.text, "html.parser")
    div = soup.find("div", { "id": "download-wrapper" })
    link = div.findAll("a")[0]['href']
    urllib.request.urlretrieve(link, file_name)

This method maybe can help you. Good luck:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM