2011-09-14 80 views

回答

1

如何findNext

import re 
from BeautifulSoup import BeautifulSoup 

html = '''<strong>Text:</strong> 

     <a href='http://domain.com'>url</a>''' 

soup = BeautifulSoup(html) 
label = soup.find("strong" , text='Text:') 
contact = label.findNext('a') 

if contact.get('href') != None: 
    print contact 
else: 
    print "No href" 

如果你專門爲一個a標籤與href,使用尋找:

contact = label.findNext('a', attrs={'href' : True}) 

有了這個,你將不再需要凝結空白。我想你是這麼做的,因爲next正在返回標籤後面的空格。