AttributeError:“命名空间”对象没有属性

时间:2020-04-07 23:18:48

标签: python namespaces

我已尽一切可能尝试了所有语言的阅读以解决该错误,但未发现任何错误。我正在做一个Web抓取工具,它使用下面的3个文件,但是在运行它时,我在cmd (我正在使用python 3.7通过anaconda实现它)中收到此错误

Traceback (most recent call last):
  File "main.py", line 23, in <module>
    _news_scraper(args.news_site)
AttributeError: 'Namespace' object has no attribute 'news_site' 

代码的3个文件是

config.yaml:

news_sites:
  eluniversal:
    url: https://www.eluniversal.com
  elespectador:
    url: https://www.elespectador.com

common.py:

import yaml

__config = None

def config():
    global __config
    if not __config:
        with open('config.yaml', mode = 'r') as f:
            __config = yaml.load(f, Loader = yaml.FullLoader)
    return __config

main.py:

import argparse
import logging
logging.basicConfig(level=logging.INFO)

from common import config

logger = logging.getLogger(__name__)

def _news_scraper(news_site_uid):
    host = config() ['news_sites'] [news_site_uid] ['url']

    logging.info('Beginning scraper for {}'.format(host))

if __name__=='__main__':
    parser = argparse.ArgumentParser()

    news_site_choices = list(config()['news_sites'].keys())
    parser.add_argument('news_site ',
                        help = 'The news site that you want to scrape',
                        type = str,
                        choices = news_site_choices)
    args = parser.parse_args()
    _news_scraper(args.news_site)

0 个答案:

没有答案