2017-02-15 95 views
-2

我剛開始python爬行,並試圖抓取web文本一個月。 我用python 2.7.13試過這段代碼,它運行得很好。AttributeError:'NoneType'對象沒有'python 2.7'使用BeautifulSoup屬性'find'

class IEEECrawler: 
    def __init__(self): 
     self.baseUrl = "http://ieeexplore.ieee.org" 
     self.targetUrl = "http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?reload=true&filter%3DAND%28p_IS_Number%3A4359286%29&rowsPerPage=100&pageNumber=1&resultAction=REFINE&resultAction=ROWS_PER_PAGE&isnumber=4359286#1.html" 
     self.soup = BeautifulSoup(urllib.urlopen(self.targetUrl).read(), "lxml") 
     self.doc_list = self.soup.find_all('div', {'class': "txt"}) 
     self.subUrl = [] 

    def crawlOriginalPage(self): 
     file = open("./result.txt", "w") 
     for doc in self.doc_list: 
      head = doc.find("h3") 
      author_list = '' 
      for author in doc.find_all("div", {'class':"authors"}): 
       for tt in author.find_all('span', {'id':"preferredName"}): 
        author_list += tt['data-author-name'] + ";" 
      author_list = author_list[:-1] 
      file.write(head.find("span").text + ';') 
      file.write(author_list.strip() + ';') 
      file.write(self.baseUrl+head.find('a')['href']+ ';') 
      file.write(doc.find("div", {'class': "hide abstract RevealContent"}).find("p").text.replace('View full abstract'+'»'.decode('utf-8'),'').strip()+ '\n') 
     file.close() 
     print 'finish' 

但是,今天我再次運行此代碼,我沒有與這個錯誤masseges工作。我無法弄清楚應該修復哪些代碼。

Traceback (most recent call last): 
    File "/Users/user/Downloads/ieee_fin/ieee.py", line 35, in <module> 
    crawler.crawlOriginalPage() 
    File "/Users/user/Downloads/ieee_fin/ieee.py", line 29, in crawlOriginalPage 
    file.write(doc.find("div", {'class': "hide abstract RevealContent"}).find("p").text.replace('View full abstract'+'»'.decode('utf-8'),'').strip()+ '\n') 
AttributeError: 'NoneType' object has no attribute 'find' 
+0

'doc'爲空。調試以找出原因。 – Carcigenicate

回答

0

錯誤顯示你行:

file.write(doc.find("div", {'class': "hide abstract RevealContent"}).find("p").text.replace('View full abstract'+'»'.decode('utf-8'),'').strip()+ '\n') 

只要看看方法find(有2個),並檢查前會發生什麼。

也許這是不好:

doc.find(...) 

這將意味着doc是類型NoneType,這將意味着docNone

或者,也許這是不好:

doc.find("div", {'class': "hide abstract RevealContent"}).find("p") 

這將意味着doc.find(...class...)正在恢復None。可能是因爲找不到任何東西。底線,您可能需要在該代碼的周圍放置一個try...except包裝,或者將其分解一點,然後開始檢查None

+0

謝謝你,奧斯汀。你的評論真的很有幫助。我會盡力並在稍後發佈。 –

相關問題