2017-07-26 207 views
1

我不斷收到錯誤‘失蹤1個人需要位置參數:‘section_url’’的findall錯誤 - NoneType」對象有沒有屬性‘的findall’

我每次嘗試的findall工作,我得到這個錯誤。

新學習python,所以任何幫助將不勝感激!

from bs4 import BeautifulSoup 
import urllib3 


def extract_data(): 

    BASE_URL = "http://www.chicagotribune.com/dining/ct-chicago-rooftops-patios-eat-drink-outdoors-near-me-story.html" 

    http = urllib3.PoolManager() 
    r = http.request('GET', 'http://www.chicagotribune.com/dining/ct-chicago-rooftops-patios-eat-drink-outdoors-near-me-story.html') 
    soup = BeautifulSoup(r.data, 'html.parser') 
    heading = soup.find("div", "strong") 
    category_links = [BASE_URL + p.a['href'] for p in heading.findAll('p')] 
    return category_links 
    print(soup) 


extract_data() 

回答

0

通常,NoneType object has no attribute類錯誤是指上游函數返回None,那麼你沒有檢查它,並試圖訪問其方法:

stuff = get_stuff() # this returns None 
stuff.do_stuff() # this crashes 

最有可能的,圖書館找不到標題爲soup.find。嘗試使用soup.select('div.strong')代替。

更多選擇: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors

更多NoneType: https://docs.python.org/3.6/library/constants.html#None

+0

非常感謝你的回答是什麼!我嘗試過,但不幸的是仍然有相同的錯誤。每次我嘗試使用findAll,我得到這個錯誤。 –

1

大廈接受的答案的答案,我想這是你想要

from bs4 import BeautifulSoup 
import urllib3 

def extract_data(): 

    BASE_URL = "http://www.chicagotribune.com/dining/ct-chicago-rooftops-patios-eat-drink-outdoors-near-me-story.html" 

    http = urllib3.PoolManager() 
    r = http.request('GET', 'http://www.chicagotribune.com/dining/ct-chicago-rooftops-patios-eat-drink-outdoors-near-me-story.html') 
    soup = BeautifulSoup(r.data, 'html.parser') 
    heading = soup.select('div strong') 
    print(heading) 
    category_links = [BASE_URL + p.a['href'] for p in [i for i, x in enumerate(heading) if x == "p"]] 
    return category_links 


print(extract_data()) 
+0

如果不是,那麼分享你所期望的'extract_data()'的輸出。 –

+0

謝謝你謝謝你!正是我想要的。我非常感謝幫助! –

相關問題