2012-02-06 39 views
0

我正在登錄到網站,進行搜索查詢,然後使用beautifulsoup過濾結果以獲取「b」標記中的所有術語。從結果中,我想檢查搜索項(測試)是否存在。我目前的代碼如下。我遇到的問題是,即使有結果並且存在這個詞,我仍然得不到迴應。我打印了過濾後的查詢並通過它讀取,結果肯定存在,因此錯誤在搜索部分。我認爲問題在於,html這個單詞測試本身並不是它自己的Testing.example或Testing.test,因此搜索無法通過它自身被空間包圍來找到它。如何在較長的單詞/短語內搜索單詞/短語。使用python和beautifulsoup搜索解析的網頁時出錯

我需要「測試」,在「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>, 

當我打印過濾我得到了上述結果。它稍長一點,但你明白了。從**的底部開始五行,您會看到結果應該是正面的,但不是。

+0

當你打印出「過濾」的值時,它是什麼?能夠添加'print'語句(或函數)來顯示中間結果是很重要的。 – 2012-02-06 11:03:40

+0

@ S.Lott我編輯了我的問題並加入了打印結果。 – 2012-02-06 11:13:53

回答

1

我相信你想要的東西更像以下

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文件可 here

+0

謝謝。這是完美的,你的解釋是有道理的。謝謝。 – 2012-02-06 12:24:14

0

警告說:我沒有進入BeautifulSoup的細節。

filteredb元素的列表。你錯過了一個關卡。試試這個:

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

對每個元素返回「not found」「found」。所以就像11個沒有找到的或者有很多b元素一樣。但它仍然會造成假陰性。我只需要它來搜索整體。 – 2012-02-06 11:40:26

+0

@Michael:我沒有在我提供的4行代碼中的任何地方看到「未找到」。請解釋! – 2012-02-06 20:20:12

+0

我添加了一個未找到的帳戶以查找未找到的案例。我以另一種方式解決了這個問題謝謝 – 2012-02-07 20:23:25

0
filtered = soup.findAll('b') 

會給你喜歡[one, two]結果。

你需要比較過濾用言語

你可以嘗試類似的內容:

soup.findAll(text=words) 
+0

請詳細說明 – 2012-02-06 11:51:19