0

I have some code which creates a list of the links on a website. At the end of the function I return the list, but when I reference it later on, the list is blank.

Here is my code:

PagesList = []
Startup = input('What is the website of the startup?')
def GetCleanLinks(Startup):
    headers = {'User-Agent': 'Mozilla/5.0'}
    response = requests.get(Startup, headers=headers)
    soup = BeautifulSoup(response.text, "html.parser")
    PagesList = [a['href'] for a in soup.find_all('a', href=True) if a.text.strip()]
    for i, link in enumerate(PagesList):
        if link[0] in ['/','#']:
            PagesList[i] = Startup + link
    print(PagesList)
    return(PagesList)

GetCleanLinks(Startup)
print(PagesList)

If I put the print within the function (before my return request) it will print out a list of the links. However, the print request outside the function prints a blank list. What am I missing?

Many thanks,

Rob

rlou
  • 498
  • 4
  • 16
  • 9
    assign `PagesList = GetCleanLinks(Startup)`, you have 2 separate variables, one global (empty, remove it at start) and one local. – Jean-François Fabre Feb 06 '18 at 16:47
  • 2
    Possible duplicate of [Using global variables in a function other than the one that created them](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – GSazheniuk Feb 06 '18 at 16:53
  • @Jean-FrançoisFabre - this made sense. I thought I may have got confused with local/global variables – rlou Feb 06 '18 at 17:07

0 Answers0