1

I have a python script called dlimage. I want to type a variable in terminal like this $ python dlimage.py 1 2 and have 1 and 2 correspond to the the url in download_web_image to become http://www.example.com/1/2.jpg and download the image. How do I go about doing this?

import urllib.request
import argparse

def download_web_image(url):
    urllib.request.urlretrieve(url)

parser = argparse.ArgumentParser()
parser.add_argument("num1", "num2")
args = parser.parse_args()

download_web_image("http://www.example.com/"num1"/"num2".jpg")

EDIT 2: I finally got it to work. Thanks everyone for your help! Code that worked:

import urllib
import argparse

def download_web_image(url):
    IMAGE = url.rsplit('/',1)[1]
    urllib.urlretrieve(url, IMAGE)

parser = argparse.ArgumentParser()
parser.add_argument("num1")
parser.add_argument("num2")
args = parser.parse_args()

download_web_image("https://www.example.com/{num1}/{num2}.jpg".format(num1=args.num1, num2=args.num2))
John P.
  • 215
  • 3
  • 10

5 Answers5

2

Change this

parser.add_argument("num1, "num2")

to

parser.add_argument("num1", "num2")
Atul Mishra
  • 298
  • 2
  • 11
  • 1
    both give errors. When defining a `positional` argument, you give only one string, the `dest`. – hpaulj Oct 26 '16 at 05:28
2

When I try part of your code, I get an error:

In [1663]: parser = argparse.ArgumentParser()
In [1664]: parser.add_argument("num1", "num2")
....
ValueError: invalid option string 'num1': must start with a character '-'

The arguments to the add_argument method are wrong.

What you should be using is:

parser = argparse.ArgumentParser()
parser.add_argument("num1")
parser.add_argument("num2")

In which case the help will look like:

In [1668]: parser.print_help()
usage: ipython3 [-h] num1 num2

positional arguments:
  num1
  num2

optional arguments:
  -h, --help  show this help message and exit

and testing an input equivalent to myprog 1 2

In [1669]: args = parser.parse_args(['1','2'])
In [1670]: args
Out[1670]: Namespace(num1='1', num2='2')
In [1671]: args.num1
Out[1671]: '1'
In [1672]: args.num2
Out[1672]: '2'

Now I can format a URL with:

In [1675]: "https://www.example.com/{}/{}.jpg".format(args.num1, args.num2)
Out[1675]: 'https://www.example.com/1/2.jpg'

So there are 2 problems with your code:

Each argument, num1 and num2 has to be defined in a separate add_argument statement. Read the docs to see what else you can add to that statement, such as the help. You are trying to define 2 arguments in one statement, and getting an error.

Secondly you need to use a correct format. I added the {} ({0} and {num1} styles also work). OR in the older Py2 style:

"https://www.example.com/%s/%s.jpg"%(args.num1, args.num2)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Hi, thanks for the help. Do you know why I'm still getting the error posted above? I'm using Py3 and the urllib.request is in the standard python 3 library, so I'm not sure where I am wrong. – John P. Oct 26 '16 at 06:04
1

for this I would use format:

download_web_image("http://www.example.com/{num1}/{num2}.jpg".format(num1=args.num1, num2 = args.num2))

Here is an example:

num1 = 5
num2 = 6
"http://www.example.com/{num1}/{num2}.jpg".format(num1 = num1, num2 = num2)

output:
'http://www.example.com/5/6.jpg'

format makes it easy to insert defined parameters into a string.

RDizzl3
  • 846
  • 8
  • 18
  • hello, I tried it and it is still not working. I've posted my edited code above. – John P. Oct 26 '16 at 04:25
  • 1
    The the format function is attached to the string not outside like this: `"http://www.example.com/{num1}/{num2}.jpg".format(num1 = num1, num2 = num2)`. It needs to be `download_web_image("http://www.example.com/{num1}/{num2}.jpg".format(num1=args.num1, num2 = args.num2))` – RDizzl3 Oct 26 '16 at 04:28
1

Assuming you are using python3, I tried to make something simple and as close to the approach you were trying originally. Hope this helps.

import urllib.request
import sys
def download_web_image(url):
    urllib.request.urlretrieve(url)

download_web_image("http://www.example.com/{0}/{1}.jpg".format(sys.argv[1], sys.argv[2]))
Wes
  • 648
  • 7
  • 14
0

I imagine you want to pass N arguments so to generate an URL I would do this :

import sys
args = sys.argv[1:]
baseURL = "http://www.example.com"
url = baseURL + "/" + "/".join(args)
print url

INPUT:

$ python dlimage.py 1 2 3.jpg

OUTPUT:

http://www.example.com/1/2/3.jpg

As for the download, you could try this answer, or read data at the url and write it as a file named as the last argument :

import urllib2
urlResponse = urllib2.urlopen(url)
urlData = urlResponse.read()

outFile = open(args[-1], 'w')
outFile.write(urlData)
outFile.close()

I'm behind a work proxy and I get an error when I download the file, might update my answer later this day at home.

Community
  • 1
  • 1
nemo9955
  • 126
  • 1
  • 8