首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >findall错误- NoneType‘对象没有属性'findall’

findall错误- NoneType‘对象没有属性'findall’
EN

Stack Overflow用户
提问于 2017-07-26 11:45:45
回答 2查看 1.6K关注 0票数 1

我一直收到错误消息"missing 1 required positional:'section_url'“

每次我尝试使用findall时,我都会得到这个错误。

刚开始学习python,如果有任何帮助,我们将不胜感激!

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

发布于 2017-07-26 12:13:39

基于Accepted的答案,我认为这就是你想要的

代码语言:javascript
复制
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())
票数 1
EN

Stack Overflow用户

发布于 2017-07-26 11:52:19

通常,NoneType object has no attribute类的错误意味着上游函数返回None,然后您没有检查它,而是试图访问它的方法:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45317069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档