2017-03-02 75 views
0
import re, time, _thread 
import urllib.request 
from bs4 import BeautifulSoup 


def get_data(n): 
    global s,r 
    html=urllib.request.urlopen('http://www.fmkorea.com/index.php?mid=best&listStyle=webzine&page='+str(n)) 
    soup=BeautifulSoup(html,'lxml') 
    l=soup.findAll('h3', {'class':'title'}) 

    for i in l: 
     for j in re.split(r'''\)|\(|\'|\"|\?|\]|\[|,|\.|\ |\:''',i.text[:i.text.rfind('[')].strip()): 
      s[j.strip()] = s.get(j.strip(),0) + 1 
      r=r+1 

    s={} 
    r=0 

    for _ in range(1,2037): 
      _thread.start_new_thread(get_data, (_,)) 
      time.sleep(0.05) 

     while r!=2036: 
      time.sleep(3) 

    with open('res','w') as f: 
     s=sorted(s.items(),key=lambda x: x[1],reverse=True) 
     for i in s: 
      f.writelines(str(i[0]) + " : " + str(i[1])+"\n") 

AttributeError       Traceback (most recent call last) 
<ipython-input-24-3e6cc449e797> in <module>() 
    35 # 
    36 with open('res','w') as f: 
---> 37  s=sorted(s.items(),key=lambda x: x[1],reverse=True) 
    38  for i in s: 
    39   f.writelines(str(i[0]) + " : " + str(i[1])+"\n") 

AttributeError: 'list' object has no attribute 'items' 

我不斷收到此錯誤上面的代碼似乎並不能修復它。我如何防止AttributeError被提出?AttributeError的:「元組」對象有沒有屬性「項目」

+1

什麼是S的內容?我認爲s是一個列表而不是數組/元組,正如錯誤所示。 – zoubida13

+0

s是字典...... – kimchiman

+0

您發佈的代碼包含一個IndentationError。因爲你沒有顯示真正的錯誤是無法證實的。請[編輯]問題,以便它可以複製和粘貼(這樣我們也可以看到異常!)。它應該只包含產生錯誤所需的必要代碼(因此可以完全刪除文件打開)。請看[mcve],當你編輯問題時,請確保你提供了一個。 – MSeifert

回答

0

項目是字典你不能使用它在列表

>>> d = {1:'a'} 
>>> d.items() 
dict_items([(1, 'a')]) 

>>> l = [1,2] 
>>> l.items() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'list' object has no attribute 'items' 
+0

s是字典。我不知道爲什麼會出現這個錯誤。 – kimchiman

相關問題