2017-07-26 60 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

谢谢你谢谢你!正是我想要的。我非常感谢帮助! –

相关问题