Recursive algorithms are used to reduce large problems to smaller ones that have the same structure, and then combine the results. They often consist of a base case that does not lead to recursion, and another case leads to recursion. For example, say you were born in 1986 and you want to calculate your age. You can write:
def myAge(currentyear): if currentyear == 1986:
I myself do not really see the point of using recursion in your problem. My suggestion, first of all, is that you set a limit in your code. What you gave us will work endlessly because the program is stuck in endlessly nested loops; he never reaches the end and begins to return. Thus, you can have a variable outside the function, which is updated every time you go down to the level, and at some point stops the function from starting a new for loop and starts returning what it found.
But then you get into changing global variables, you use recursion in a weird way, and the code gets messy.
Now, after reading the comments and seeng, what you really want, which, I must say, is not entirely clear, you can use the help from the recursive algorithm in your code, but not write it all recursively.
def recursiveUrl(url,depth): if depth == 5: return url else: page=urllib2.urlopen(url) soup = BeautifulSoup(page.read()) newlink = soup.find('a')
Now there is still a problem: links are not always linked to web pages, but also to files and images. This is why I wrote the if / else statement in the recursive part of the "open URL" function. Another problem is that your first website has 2166 links to institutions, and creating 2166 * 5 beautiful photos is not fast. The above code performs a recursive function 2166 times. This should not be a problem, but you are dealing with large html (or php) files, so the 2166 * 5 soup takes a huge amount of time.
Jallo
source share