VipinSaini
VipinSaini

Reputation: 23

For loop in Function returning only one value in Python

I am new to Python. I am trying to fetch URLs from a page, which has 18 URLs in it, in a DIV with all having the same class name. Below is the code I have used. When I use the below code without a return statement. then it gives all 18 URLs on the page. I have to return these URLs and when I am doing so, it's only returning one URL.

URL = 'https://www.example.com/destinations/'

def make_soup(URL):
  response = requests.get(URL)
  soup = BeautifulSoup(response.content, 'html.parser')
  return soup
  
def get_new_urls(soup):
  for links in soup.find_all("div", class_="col-sm-2 col-md-2 col-lg-2 col-xl-2 col-xs-12 col-6 p0 mb-25"):
    dlinks=links.a['href']
  return dlinks



make_soup(URL)
get_new_urls(soup)

Pls, help me find solution to this problem. Thanks in advance!

Upvotes: 0

Views: 279

Answers (1)

chsws
chsws

Reputation: 443

You need to collect the results in another object. First create the list, use .append to add to it, then return the new, populated list.

def get_new_urls(soup):
    dlinks = [] # create empty list to collect results
    for links in soup.find_all("div", class_="col-sm-2 col-md-2 col-lg-2 col-xl-2 col-xs-12 col-6 p0 mb-25"):
        dlinks.append(links.a['href']) # add results to the list
    return dlinks # return the full list

Upvotes: 1

Related Questions