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