2013-04-23 14 views
1

我需要找到具有任何鼠標事件註冊在線 例如所有標籤的所有標籤,它應該找到標籤,如:使用beautifulsoup找到與某種形式的屬性

<div onmousedown="somefunc()"> Some text here </div> 

我可以檢查使用功能如bs4文檔中給出的:

def reg_event(tag): 
    return tag.has_key('onmousedown') 
tags_with_mouse_event = soup.find_all(reg_event) 

但檢查多個需要很多.has_key結合或..是否有一些更簡單的方法?

回答

0

你可以挑選出所有的標籤與on..屬性,如:

soup.find_all(lambda tag: any(attr.startswith('on') for attr in tag.attrs.keys())) 

否則,請你在做什麼事件後set,做一些如:

events = {'onmousedown', 'onmouseup', 'onclick'} # etc... 
soup.find_all(lambda tag: any(attr in events for attr in tag.attrs.keys())) 
相關問題