It was difficult to understand the question, so I will write down the code and the results I want.
·I mistook it for recursive, so I changed the title as well
What do you want to do in the end
·I want to display the results of this link on the web screen using Bottle
Tried
from urlib.request import urlopen
from bs4 import BeautifulSoup
import datetime
import random
import re
from bottle import route, view
random.seed(datetime.datetime.now())
defgetLinks(articleUrl):
html=urlopen("http://en.wikipedia.org"+articleUrl)
bsObj=BeautifulSoup(html, "html.parser")
return bsObj.find("div", {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)(?!)*$"))
@route('/')
@view("test_template")
def test():
links=getLinks("/wiki/Kevin_Bacon")
results = [ ]
whilelen (links) > 0:
newArticle=links [random.randint(0,len(links)-1)].attrs ["href" ]
results.append(newArticle)
links = getLinks (newArticle)
dict(results=results)
·Examples of screen display results
['/wiki/Randy_Quaid']
·The above was passed to the template at the time of the loop, so I would like to collect the results and give them to the template after the loop ends, so when I changed it to the following, I fell into an infinite loop, but is it Naze?
whilelen(links)>0:
newArticle=links [random.randint(0,len(links)-1)].attrs ["href" ]
results.append(newArticle)
links = getLinks (newArticle)
dict(results=results)
·Image of the result you want in the end (it's just the expected range)
['/wiki/Richard_Chamberlain', '/wiki/Richard_Chamberlain']
The following initial questions
·What should I do if I want to obtain the following code together after the loop ends instead of outputting it at any time during the loop?
def getHoge(p):
return treatment
res=getHoge("string")
def hoge():
whilelen(res)>0:
str = processing
print(str)
links = getHoge(str)
hoge()
·Retrieved as an object composed of strings?
*In the first place, I don't really understand the format of acquisition itself
·I tried but it doesn't work (infinite loop?) code
def getHoge(p):
return treatment
res=getHoge("string")
result=['1']#Variable definition. It said that it cannot be defined without substitution, so I just substituted '1' for now.
def hoge():
global result
whilelen(res)>0:
str = processing
print(str)
result.append(str)
links = getHoge(str)
retuen result# I want to get it here
hoge()
I can't get the hang of the question. Is this what you mean?
def getHoge(p):
return treatment
defaulthoge(res, results=[]):
iflen(res)>0:
str = processing
results.append(str)
hoge(getHoge(str), results)
return results
hoge(getHoge("string"))
The original code does not update res
, so if len(res)>0
first, it will be an infinite loop.
Also, I didn't know what the variable links
meant, so I thought of it as links==res
.
How about the following
def getHoge(p):
return treatment
def hoge():
results = [ ]
res=getHoge("string")
whilelen(res)>0:
str = processing
results.append(str)
res=getHoge(str)
return results
results=hoge()
© 2024 OneMinuteCode. All rights reserved.