how to find not the next sibling but the 2nd sibling or find sibling "a" OR sinbling "b"

Kent Johnson kent at
Thu Jan 19 12:07:23 CET 2006

localpricemaps at wrote:
> i have some html which looks like this where i want to scrape out the
> href stuff (the part)
> <div class="noFood">Cheese</div>
> <div class="food">Blue</div>
> <a class="btn" href = "">
> so i wrote this code which scrapes it perfectly:
> for incident in row('div', {'class':'noFood'}):
> 			b = incident.findNextSibling('div', {'class': 'food'})
>        			print b
> 			n = b.findNextSibling('a', {'class': 'btn'})
>        			print n
> 			link = n['href'] + "','"
> problem is that sometimes the 2nd tag , the <div class="food"> tag , is
> sometimes called food, sometimes called drink.  

Apparently you are using Beautiful Soup. The value in the attribute 
dictionary can be a callable; try this:

def isFoodOrDrink(attr):
   return attr in ['food', 'drink']

b = incident.findNextSibling('div', {'class': isFoodOrDrink})

Alternately you could omit the class spec and check for it in code.


More information about the Python-list mailing list