Search code examples
pythonurllibfancyurlopener

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


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:\Users\Name\Anaconda3\lib\site-packages\ipykernel_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()

Solution

  • 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")