-1

Want to keep this short and sweet.

I'm trying to run the requests module, and utilise urllib on Python 3.6.3. I'm on MacOSX 10.12.

I installed Pip3 and ran the pip3 commands, such as "pip3 install requests", which should activate requests, right...?

Here's my screen when I type "pip3 freeze" if it helps.

Thanks!

jdal2700
  • 9
  • 3
  • Show us the error you get when you try to use requests. – John Gordon Oct 15 '17 at 02:32
  • Here: https://imgur.com/a/wBnPY – jdal2700 Oct 15 '17 at 02:39
  • The green represents the specific folder I was executing the command from, and the orange is specific parts of the website link. This is a test, where I was trying to extract specific image links from a website using the requests module. It worked in the past, and when I typed a value of numbers for the code to search for (seeing if jpg subdomains existed) it would cycle through them very quickly with no errors (using pip). But a week ago I accidentally deleted some stuff and now it *runs*, but gives a timeout error. Not sure what to make of this. – jdal2700 Oct 15 '17 at 02:41
  • One last thing, a sample of the actual code, in download.py: https://imgur.com/a/2i8qN – jdal2700 Oct 15 '17 at 02:44

1 Answers1

1

When you do import urllib.request you are not actually importing the requests package that you installed, instead you are just using the request module of the builtin urllib package. You should do import requests instead. You can find the requests package docs here. Here is some sample code to download a file

import requests
url = 'http://example.com/image.png'
r = requests.get(url)
with open("image.png", "wb") as image:
    image.write(r.content)

Also your output shows Operation timed out as the error, that's usually a network related issue.


Since you asked, this is how to write your code sample using requests:

import requests

start = int(input("Start range: "))
stop = int(input("End range: "))

for i in range(start, stop+1):
    filename = str(i).rjust(6, '0')+".jpg"
    url = 'https://website.com/Image_' + filename
    print(url)
    r = requests.get(url)
    with open(filename, "wb") as image:
        image.write(r.content)
Arunmozhi
  • 1,034
  • 8
  • 17
  • What would I substitute these lines with: try: urllib.request.urlretrieve(url, filename) except urllib.error.URLError as e: if I'm using "import requests"? – jdal2700 Oct 15 '17 at 03:09
  • Looks like you are just starting out in your programming journey. I am adding the code you asked. But, I strongly recommend taking your time to understanding the basics of the Python language and programming in general. The changes are very simple and you should know how it works more than what it does. – Arunmozhi Oct 16 '17 at 08:35