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

2

4

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()
Bolshevist answered 18/1, 2019 at 21:7 Comment(0)
A
5

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")
Aloha answered 18/1, 2019 at 21:18 Comment(5)
Thanks for the tip! I managed to get it working with the 'requests' library instead however.Bolshevist
For me, urllib2 was giving a squiggly line in my VS Code IDE I changed urllib2 to urllib and it worked, based off this answerBagwell
@Bolshevist Don't update your question with the answer, but you can post your own answer.Calcariferous
urllib2 only exists in Python 2. Modern Python 3 simply has urllib.Calcariferous
Neither urllib2 nor urllib works in base Python 3Jochebed
B
0

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)
Bagwell answered 22/12, 2022 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.