lambda - simplify this python generation -


i new python. there way simplify this:

def getdivs():     divs = soup.findall(name = "div", attrs = {"class" : "resultcell"}, recursive = true)     div in divs:         h2 = div.find("h2")         = h2.find("a")         href = a["href"]         yield (href)  divs = list(getdivs()) 

i feel should able create anonymous function instead of getdivs. (pseudocode):

divs =     [      divs = soup.findall(name = "div", attrs = {"class" : "resultcell"}, recursive = true)      div in divs:         h2 = div.find("h2")         = h2.find("a")         href = a["href"]         yield (href)   ] 

thanks

just use list comprehension:

divs = [ div.find("h2").find("a")["href"]            div in soup.findall(name = "div",                                    attrs = {"class" : "resultcell"},                                    recursive = true) ] 

but using proper xml parsing tools better idea.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -