web scraping - Different ouput in similar code - Webscraping with Python -


in this link, try count numbers appear links above table of webpage. check link have better idea. have 2 codes similar, first code gives expected output. so, wrong second code?

import urllib2 bs4 import beautifulsoup  soup = beautifulsoup(urllib2.urlopen("http://www.admision.unmsm.edu.pe/admisionsabado/a/011/0.html")) c=[] n in soup.find_all('center'):     b in n.find_all('a')[1:]:         c.append(b.text)  t = len(c) / 2  print t 

the result 41. in webpage there 41 numbers appear links above table of webpage, output right.

in wrong code, define function input subset of url. code below:

import urllib2 bs4 import beautifulsoup   def record(part):     soup = beautifulsoup(urllib2.urlopen("http://www.admision.unmsm.edu.pe/admisionsabado".format(part)))     c=[]     n in soup.find_all('center'):         b in n.find_all('a')[1:]:             c.append(b.text)      t = len(c)/2     print t 

as see method counting numbers same. so, run function:

record('/a/011/0.html') 

unfortunately, output 0.

inside function, formatting url string passed in parameter, format string didn't have placeholder {} in place value at. here it.

"http://www.admision.unmsm.edu.pe/admisionsabado{}".format(part)


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -