2012-10-24 206 views
4
之一

是否有可能修改這個不知何故,以便它可以找到表類class1的 * *類類class2檢查類是相等的兩個字符串

Info = soup.find('table', {'class' :'class1'}) 
+0

我還沒有真正使用過美麗的湯,但也許[這個答案](http://stackoverflow.com/a/706531/1460235)將有所幫助。你可以傳入一個搜索這兩個類的函數嗎? –

回答

2
find_all(self, name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs) 

    Extracts a list of Tag objects that match the given 
    criteria. You can specify the name of the Tag and any 
    attributes you want the Tag to have. 

    The value of a key-value pair in the 'attrs' map can be a 
    string, a list of strings, a regular expression object, or a 
    callable that takes a string and returns whether or not the 
    string matches for some custom definition of 'matches'. The 
    same is true of the tag name. 

例如:

>>> from bs4 import BeautifulSoup 
>>> text = ''.join('<table class="class{}"></table>'.format(i) for i in range(10)) 
>>> soup = BeautifulSoup(text) 
>>> 
>>> soup.find_all("table", {"class": ["class1", "class7"]}) 
[<table class="class1"></table>, <table class="class7"></table>] 
>>> import re 
>>> soup.find_all("table", {"class": re.compile("class[17]")}) 
[<table class="class1"></table>, <table class="class7"></table>] 
>>> 
>>> soup.find_all("table", {"class": lambda x: 3*int(x[-1])**2-24*int(x[-1])+17 == -4}) 
[<table class="class1"></table>, <table class="class7"></table>] 

[好吧,最後一個比賽太多了,但你的想法:任何BOOL,返回匹配功能將工作]

+0

好東西你實際上並沒有在第一次嘗試中獲得最後的功能,或者我不得不給你+2,這是不可能的。 :) – abarnert

相關問題