Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Downloaded file corrupted
#1
Hello,

I am trying to download a file from a web page, but when I try to open it, it shows an error    


This is my code:
session = requests.Session()
        retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
        session.mount('https://', HTTPAdapter(max_retries=retries))
        sw = 1
        for row in rows[4:]:
            if sw == 1:
                cells = row.find_elements(By.TAG_NAME, "td")
                if not cells:
                    continue
                else:
                    image_elements = cells[0].find_elements(By.TAG_NAME, "img")
                    image_to_click = image_elements[0]
                    link_element = cells[0].find_element(By.TAG_NAME, "a")
                    filename = link_element.get_attribute("href")
                    file_2b_downloaded = file_path+ "/" + filename.split("=")[-1]

                    response = requests.get(filename, verify=False)
                    if response.status_code == 200:
                        open(file_2b_downloaded, 'wb').write(response.content) 

                        #Creates a folder and move the respective data into it
                        
                        source_file = "C:/Users/"+ username +"/Downloads"
                        destination_folder = fp + "/FO" + rpf[j]
                        if not os.path.exists(destination_folder):
                            os.makedirs(destination_folder)
                        shutil.move(file_2b_downloaded, destination_folder)
                        
                    else:
                        print(f"Failed tp download. Status code: {response.status_code}")
                sw = 0
            else:
                sw = 1
How can I fix this?

Thanks in advance for the help.
Reply
#2
cross-posted a https://stackoverflow.com/q/77205487/4046632
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Sep-30-2023, 05:10 AM)buran Wrote: cross-posted a https://stackoverflow.com/q/77205487/4046632
Yes, I need urgent assistance on this and I deleted the post from the other one.
Reply
#4
Can you post url to page you try to download from?
It is hard to advice if don't know what webpage use for download code.
Reply
#5
(Sep-30-2023, 01:21 PM)snippsat Wrote: Can you post url to page you try to download from?
It is hard to advice if don't know what webpage use for download code.

That would be the problem, that URL cannot accessed from Internet but an Intranet.
Reply
#6
emont Wrote:That would be the problem, that URL cannot accessed from Internet but an Intranet.
Ok,to look a little more at this,dos file_2b_downloaded give you the real download link?
To give a example,you should test like this no loop or other stuff,just test the download.
Sample zip files download
import requests

url = 'https://drive.google.com/uc?export=download&id=1o9DtaYEb1N-C_L7kqCAgfE0D5RaEwbZH'
response = requests.get(url)
with open('5mb.zip', 'wb') as f:
    f.write(response.content)
So this works now because here i use the real download link,for the 5 mb sample zip file.

If parse out the link,look like this
import requests
from bs4 import BeautifulSoup

url = 'https://web-utility.com/en/sample/files/sample-zip-file-download'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
link = soup.select_one('div.col-md-3.col-3.text-right > a').get('href')
response = requests.get(link)
with open('5mb.zip', 'wb') as f:
    f.write(response.content) 
So this is a simpler case because download link is exposed and all can see it.
One more here i look at what is send over network Chrome dev-tool(inspect -> network),sometime can need to do this.
import requests

headers = {
    'authority': 'drive.google.com',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
}

params = {
    'export': 'download',
    'id': '1o9DtaYEb1N-C_L7kqCAgfE0D5RaEwbZH',
}

response = requests.get('https://drive.google.com/uc', params=params, headers=headers)
with open('5mb.zip', 'wb') as f:
    f.write(response.content)
Also some cases can be using Selenium be easier,as if have to press a button before get the real download address.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I downloaded a script and I cant run this luoxr18 3 1,215 Apr-16-2023, 09:33 AM
Last Post: Larz60+
Sad pandas writer create "corrupted" file freko75 1 2,828 Jun-14-2022, 09:57 PM
Last Post: snippsat
  Structuring and pivoting corrupted dataframe in pandas gunner1905 2 2,252 Sep-18-2021, 01:30 PM
Last Post: gunner1905
  How to create local copies of Python packages so they do not have to be downloaded okhajut 3 2,038 Sep-29-2020, 02:22 PM
Last Post: buran
  Error in Python3.6:free() Corrupted unsorted chunks error sameer_k 2 3,875 Mar-18-2020, 09:37 AM
Last Post: sameer_k
  How to create an environment for program downloaded from github StartedNewLife 3 2,197 Jan-25-2020, 06:34 PM
Last Post: ibreeden
  Multiprocessing.Queue Issues (Missing/Corrupted Items/No Output) python3noob 0 3,214 Aug-03-2019, 09:38 PM
Last Post: python3noob
  Why the file is not downloaded in full with this script? Jacklops 2 2,713 Jan-07-2019, 09:33 AM
Last Post: DeaD_EyE
  PyPDF2 Hanging When Trying to Open Corrupted PDF bmccollum 6 8,788 Nov-09-2018, 10:40 AM
Last Post: Larz60+
  importing downloaded usb module kiyoshi7 5 4,437 Apr-29-2018, 06:44 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020