2012-01-25 8 views
20

I持って、次のHTML(\ nの付いた改行):美味しいスープで特定のテキストのタグを見つけるには?

... 
<tr> 
    <td class="pos">\n 
     "Some text:"\n 
     <br>\n 
     <strong>some value</strong>\n 
    </td> 
</tr> 
<tr> 
    <td class="pos">\n 
     "Fixed text:"\n 
     <br>\n 
     <strong>text I am looking for</strong>\n 
    </td> 
</tr> 
<tr> 
    <td class="pos">\n 
     "Some other text:"\n 
     <br>\n 
     <strong>some other value</strong>\n 
    </td> 
</tr> 
... 

どのように私はを探していますテキストを検索するには?以下のコードは最初に見つかった値を返します。したがって、フィルタリングする必要があります。何らかの形で固定テキスト

result = soup.find('td', {'class' :'pos'}).find('strong').text 

UPD。私は、次のコードを使用している場合:

title = soup.find('td', text = re.compile(ur'Fixed text:(.*)', re.DOTALL), attrs = {'class': 'pos'}) 
self.response.out.write(str(title.string).decode('utf8')) 

は、それだけで固定テキストを返します。

+0

あなたは 'findAll'を使う必要があります。 – JBernardo

答えて

関連する問題