2012-02-06 8 views
0

私は検索クエリを作成してから、結果をbeautifulsoupでフィルタリングして "b"タグのすべての用語を取得しています。結果から、検索用語(Testing)が存在するかどうかをチェックしたいと思います。私の現在のコードは以下の通りです。私が抱えている問題は、結果があり、その言葉が現れても、まだ現状の回答が得られないということです。私は、フィルタリングされたクエリを印刷し、それを読み取って、結果が間違いなくそこにあるので、エラーが検索部分にあります。私は問題は、htmlでは単語のテスト自体がTest.exampleまたはTesting.testではないので、検索ではスペースで囲まれた自己では見つけることができないということです。長い語句内の語句を検索するにはどうすればよいですか?pythonとbeautifulsoupで解析されたWebページを検索する際にエラーが発生しました

私は理にかなっている「example.Testing.example」または「test.Testing.example」

希望に発見される「テスト」が必要です。

おかげ

words = ["Testing"] 
br.open ('http://www.example.com/browse.php?psec=2&search=%s' % words) 
html = br.response().read() 
soup = BeautifulSoup(html) 
filtered = soup.findAll('b') 

# print filtered 

for word in words: 
    if word in filtered: 
     print "%s found." % word 
    else: 
     print "%s not found." % word 

編集

[<b><a title="Unknown">---</a></b>, <b>Welcome Back<br /><a href="/user/">< 
span style="color:#0080FF;"></span></a>!<br /></b>, <b><span class="smallfo 
nt"><a href="/messages.php?action=viewmailbox"><img height="14px" style="border: 
none" alt="inbox" title="inbox (no new messages)" src="/pic/pn_inbox.gif" /></a> 
59 (0 New)</span></b>, <b><span class="smallfont">&nbsp;&nbsp;<a href="/message 
s.php?action=viewmailbox&amp;box=-1"><img height="14px" style="border:none" alt= 
"sentbox" title="sentbox" src="/pic/pn_sentbox.gif" /></a> 37</span></b>, <b>Sho 
w all</b>, <b><< Prev</b>, <b>Next >></b>, <b>1&nbsp;-&nbsp;7</b>, **<b>The.Testing 
.example.T3Z6.L</b>**, <b><span style="color:#FF5500;">dgHn</span 
></b>, <b><a href="/details.php?id=15829&amp;hit=1&amp;filelist=1">1</a></b>, <b 
><a href="/details.php?id=15829&amp;hit=1&amp;=1"><font>30</font></a></ 
b>, <b><a href="/details.php?id=15829&amp;hit=1&amp;todlers=1">1</a></b>, 

私は、私は上記の結果を得るフィルタ印刷するとき。少し長めですが、アイデアを得ることができます。 **の下から5行は、結果が正であるが、そうでないことが分かる。

+0

'filtered'の値を出力すると、それは何ですか?中間結果を表示するために 'print'ステートメント(または関数)を追加できることが重要です。 –

+0

@ S.Lott私は自分の質問を編集し、印刷結果に追加しました。 –

答えて

1

here

+0

ありがとうございます。それは完璧に動作し、あなたの説明は理にかなっています。ありがとう。 –

0

注意:私はBeautifulSoupの詳細にはいません。

filteredは、b要素のリストです。あなたはレベルがありません。あなた[one, two]のような結果が得られます

for word in words: 
    for b_elt in filtered: 
     if word in b_elt: # or word in b_elt.text or suchlike 
      print "%s found." % word 
+0

各要素に対して「見つからない」「見つかりました」を返します。だから11が見つからないか、多くの要素があるように。それでも、偽陰性が生じる。私はそれを全体的に検索する必要があります。 –

+0

@Michael:私が提供した4行のコードのどこにも "見つからない"とは見えません。説明してください! –

+0

私はそれが見つからない場合を説明するために見つからなかったものを追加しました。私は別の方法で問題を解決しました。お返事ありがとうございます –

0
filtered = soup.findAll('b') 

:これを試してみてください。

あなたが好きな何か試すことができます言葉

でろ過の内容を比較する必要があります:私はあなたがより多くの次

words = ["Testing"] 
br.open ('http://www.example.com/browse.php?psec=2&search=%s' % words) 
html = br.response().read() 
soup = BeautifulSoup(html) 
filtered = soup.findAll('b') 
"""element.contents[0] gives you the first element inside the <b> tags 
If you want some other part of inside the b tag see 
BeatifulSoup documentation at the line below """ 
filteredcontents = [element.contents[0] for element in filtered] 

for word in words: 
    if any(word in filteredcontent for filteredcontent in filteredcontents): 
     print "%s found." % word 
    else: 
     print "%s not found." % word 

BeatifulSoupドキュメントのようなものが利用可能であるしたいと考えてい

soup.findAll(text=words) 
+0

詳細をご記入ください –

関連する問題