查看完整版本: python 爬文抓關鍵字
頁: [1]

busystudent 發表於 2015-2-5 01:47 AM

python 爬文抓關鍵字

各位版友好 我是teddy


這是又關python的問題,我寫一段程式碼想爬文抓關鍵字如圖的「毛澤東戰法」

</li><!-- annotation -->



    <li style="display: none;"  class="aItem textAnnotation" id="annotation_0_3" onmouseover="hoverHighlight(this);" onmouseout="outHighlight(this);">
      
          <div class="highlight  highlight_yellow"  id="highlight_0_3">
            <div class="diigoContent"><div class="diigoContentInner">毛澤東戰法</div></div>
            
                <div id="sticky_0_3" class="stickys" style="display:none">
                  <ul id="stickyUL_0_3" class="stickysUL"></ul>
                </div>
            
            <div id="sEditor_0_3" class="cEditor" style="display:none"></div><!--sEditor-->


          </div><!--highlight-->我的程式碼寫好了,可是會出現
IndexError                                Traceback (most recent call last)
<ipython-input-3-d61dbfb678e1> in <module>()
      3 soup = BeautifulSoup(res.text.encode("utf=8"))
      4 bid_table = soup.findAll('li',{'class':'diigoContentInner'})
----> 5 print bid_table.findAll('a',{'href':True})

IndexError: list index out of range



我的原程式碼是
import requests
import re
from BeautifulSoup import BeautifulSoup
import HTMLParser

user1 = 'busystudent'
res = requests.get("https://www.diigo.com/user/"+user1)
soup = BeautifulSoup(res.text.encode("utf=8"))
bid_table = soup.findAll('li',{'class':'diigoContentInner'})
print bid_table.findAll('a',{'href':True})請各位高手幫幫忙,感謝

sincerely
teddy
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

bagarandom 發表於 2015-2-8 12:31 PM

IndexError                                Traceback (most recent call last)
<ipython-input-3-d61dbfb678e1> in <module>()
      3 soup = BeautifulSoup(res.text.encode("utf=8"))
      4 bid_table = soup.findAll('li',{'class':'diigoContentInner'})
----> 5 print bid_table.findAll('a',{'href':True})

IndexError: list index out of range

Python不熟,不過單就錯誤訊息來看是指定的index超出陣列範圍了,
bid_table改成0試試?不過比較好的寫法應該是先檢查bid_table的長度才對。...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>
頁: [1]