Home > OS >  How does one iterate through an array to make different urls?
How does one iterate through an array to make different urls?

Time:03-03

I am trying to iterate through a little array and create urls out of the information contained therein.

Here's a dummy setup of what I'm trying to do:

import urllib.parse
DEEP_CAT_URL = 'https://google.com/search?q=%s'

def GetCatLink(cats=()):
  for cat in cats:
    cat_id = cat['name']
    color = cat.get('color')
    if color:
      deep_cat_url = DEEP_CAT_URL % urllib.parse.quote(color ' cat',safe=' ')
      return (deep_cat_url)


CATS = [
    {
        'color': 'red',
        'name': 'Redd Foxx',
    },
    {
        'color': 'black',
        'name': 'Donnie Darko',
    },
]

print("There are " str(len(CATS)) " cats to consider")

for h in range(len(CATS)):
  cnum=str(h 1)
  print("Cat #" cnum ":")
  if CATS[h]["color"] != '':
    print("The name of Cat #{} is {}. The color of {} is {}.".format(cnum,CATS[h]["name"],CATS[h]["name"], CATS[h]["color"]))
    x=CATS[h]
    print(x)
    print(GetCatLink(CATS))

It kind of works, but outputs:

There are 2 cats to consider
Cat #1:
The name of Cat #1 is Redd Foxx. The color of Redd Foxx is red.
{'color': 'red', 'name': 'Redd Foxx'}
https://google.com/search?q=red cat
Cat #2:
The name of Cat #2 is Donnie Darko. The color of Donnie Darko is black.
{'color': 'black', 'name': 'Donnie Darko'}
https://google.com/search?q=red cat

The goal here is to have two urls:

Cat#1 https://google.com/search?q=red cat

Cat#2 https://google.com/search?q=black cat

CodePudding user response:

You're looping over all cats in GetCatLink and on the first iteration, you return a URL - so you always get the URL for the first cat in CATS. There's no reason to do any looping over there, why not pass the cat you want a URL for to the function and just process that one?

Instead of:

def GetCatLink(cats=()):
  for cat in cats:
    cat_id = cat['name']
    color = cat.get('color')
    if color:
      deep_cat_url = DEEP_CAT_URL % urllib.parse.quote(color ' cat',safe=' ')
      return (deep_cat_url)

Use:

def get_cat_link(cat):
    cat_id = cat['name']
    color = cat.get('color')
    return DEEP_CAT_URL % urllib.parse.quote(color   ' cat', safe=' ')

And instead of this:

print(GetCatLink(CATS))

This:

print(get_cat_link(x))

There's more to be said about your code - there's some surplus stuff in there, but this addresses your main issue.

(As @JonClements correctly comments: you will run into trouble if you try make requests with the URLs you're creating, unless you use them in a normal browser. Google won't give you the result you expect if you just use urllib or requests without getting "tricksy".)

CodePudding user response:

The function GetCatLink and its invocation needs to be modified. Then everything works well.

def GetCatLink(cat):
    color = cat.get('color')
    if color:
        return DEEP_CAT_URL % urllib.parse.quote(color   ' cat', safe=' ')

for h in range(len(CATS)):
    cnum = str(h   1)
    print("Cat #"   cnum   ":")
    if CATS[h]["color"] != '':
        print("The name of Cat #{} is {}. The color of {} is {}.".format(cnum, CATS[h]["name"], CATS[h]["name"],
                                                                         CATS[h]["color"]))
        x = CATS[h]
        print(x)
        print(GetCatLink(CATS[h]))
  • Related