Trying to use fancyURLopener in Python3 for a PDF, but it gives me a DeprecationWarning error

Question:

I am trying to access a PDF file from a bank’s website for PDF mining, but it keeps returning HTTP 403 error. So as a workaround, I am trying to change my User-Agent to a browser for accessing the file (and downloading it).

The code below is part of what I have right now. This returns the following error:

C:UsersNameAnaconda3libsite-packagesipykernel_launcher.py:8: DeprecationWarning: MyOpener style of invoking requests is deprecated. Use newer urlopen functions/methods

How do I fix this?

import urllib.request

my_url = 'someurl here'

class MyOpener(urllib.request.FancyURLopener):
    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) 
Gecko/20071127 Firefox/2.0.0.11'

myopener = MyOpener()

page = myopener.open(my_url)
page.read()
Asked By: bullfighter

||

Answers:

You can try this:

import urllib2

def download_file(download_url):
    response = urllib2.urlopen(download_url)
    f = open("the_downloaded_file.pdf", 'wb')
    f.write(response.read())
    f.close()

download_file("some url to pdf here")
Answered By: Vaibhav

For me, urllib2 was giving a squiggly line in my VS Code IDE

I changed urllib2 to urllib and it worked, based off this answer

from urllib.request import urlopen

def http_get_save(url, encoded_path):
    with urlopen(url) as response:
        body = response.read().decode()
        with open(encoded_path, 'w') as f:
            f.write(body)
Answered By: Jesus is Lord
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.