当我尝试在Django中运行我的scraper时,出现以下错误。
当我尝试使用内置的Django模型将抓取的数据放入数据库时,发生了这种情况。
回溯和错误:
/usr/bin/python3.6 /home/xxxxxx/Desktop/project/teo/movierater/scrap.py
Traceback (most recent call last):
File "/home/xxxxxx/Desktop/project/teo/movierater/scrap.py", line 7, in <module>
from teo.movierater.api.models import *
File "/home/xxxxxx/Desktop/project/teo/movierater/api/models.py", line 3, in <module>
class Author(models.Model):
File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 64, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
我将其放入 bin / activate:
export DJANGO_SETTINGS_MODULE=mysite.settings
这是scraper.py的代码片段:
import django
import os
import requests
from bs4 import BeautifulSoup as bs
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from teo.movierater.api.models import *
os.environ['DJANGO_SETTINGS_MODULE'] = 'movierater.settings'
django.setup()
我的项目结构和设置:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'movierater.api',
]
wsgi.py文件:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'movierater.settings')
application = get_wsgi_application()
答案 0 :(得分:0)
给出您的文件夹结构,如下:
os.environ['DJANGO_SETTINGS_MODULE'] = 'movierater.settings'
应该是:
os.environ['DJANGO_SETTINGS_MODULE'] = 'movierater.api.settings'