2010-04-08 36 views

回答

8

只需稍作更改即可。

soup.findAll('meta', attrs={'name':re.compile("^description$", re.I)}) 
15

您可以給BeautifulSoup一個正則表達式來匹配屬性。類似於

soup.findAll('meta', name=re.compile("^description$", re.I)) 

可能會訣竅。從the BeautifulSoup docs打折。

9

正則表達式?現在我們有another problem

相反,你可以在一個拉姆達傳:

soup.findAll(lambda tag: tag.name.lower()=='meta', 
    name=lambda x: x and x.lower()=='description') 

x and避免異常時name屬性的標籤沒有被定義)

+2

+1用於避免正則表達式。 xkcd鏈接+1。 – FlipMcF 2013-05-04 00:45:28

+0

使用bs4我得到「find_all()得到了多個值的關鍵字參數'名稱'」與:/ – Joaolvcm 2014-02-20 11:14:08

3

隨着BS4使用以下命令:

soup.find('meta', attrs={'name': lambda x: x and x.lower()=='description'}) 
相關問題