def find_nearest(elt):
    preceding = elt.xpath('preceding::*/@href')[::-1]
following = elt.xpath('following::*/@href')
parent = elt.xpath('parent::*/@href')
for href in roundrobin(parent, preceding, following):
    return href
Найти ближайшую ссылку с BeautifulSoup (python)