我一直在尝试使用django和djangoitem建立一个scrapy蜘蛛。当我尝试运行我的蜘蛛时,item.py始终无法导入django应用程序的模型。
我设置了一个新的django网站,并按照https://stackoverflow.com/a/19073347/3988392的说明操作,我仍无法从我的应用中导入模型。我认为所有内容都是按照该主题中的说明设置的(除了我称之为testsite而不是mysite等等。)但如果有人认为它可能有用,我会发布代码。
运行scrapy crawl example
时的回溯:
Traceback (most recent call last):
File "/usr/bin/scrapy", line 9, in <module>
load_entry_point('Scrapy==0.24.4', 'console_scripts', 'scrapy')()
File "/usr/lib/pymodules/python2.7/scrapy/cmdline.py", line 143, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "/usr/lib/pymodules/python2.7/scrapy/cmdline.py", line 89, in _run_print_help
func(*a, **kw)
File "/usr/lib/pymodules/python2.7/scrapy/cmdline.py", line 150, in _run_command
cmd.run(args, opts)
File "/usr/lib/pymodules/python2.7/scrapy/commands/crawl.py", line 57, in run
crawler = self.crawler_process.create_crawler()
File "/usr/lib/pymodules/python2.7/scrapy/crawler.py", line 87, in create_crawler
self.crawlers[name] = Crawler(self.settings)
File "/usr/lib/pymodules/python2.7/scrapy/crawler.py", line 25, in __init__
self.spiders = spman_cls.from_crawler(self)
File "/usr/lib/pymodules/python2.7/scrapy/spidermanager.py", line 35, in from_crawler
sm = cls.from_settings(crawler.settings)
File "/usr/lib/pymodules/python2.7/scrapy/spidermanager.py", line 31, in from_settings
return cls(settings.getlist('SPIDER_MODULES'))
File "/usr/lib/pymodules/python2.7/scrapy/spidermanager.py", line 22, in __init__
for module in walk_modules(name):
File "/usr/lib/pymodules/python2.7/scrapy/utils/misc.py", line 68, in walk_modules
submod = import_module(fullpath)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/myUserName/test-env/testbot/testbot/spiders/example.py", line 3, in <module>
from testbot.items import UsefulModelItem
File "/home/myUserName/test-env/testbot/testbot/items.py", line 11, in <module>
from testapp.models import UsefulModel
ImportError: No module named testapp.models
├── testbot
│ ├── scrapy.cfg
│ └── testbot
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── items.py
│ ├── items.pyc
│ ├── pipelines.py
│ ├── settings.py
│ ├── settings.pyc
│ └── spiders
│ ├── example.py
│ ├── example.pyc
│ ├── __init__.py
│ └── __init__.pyc
└── testsite
├── manage.py
├── testapp
│ ├── admin.py
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ └── views.py
└── testsite
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
└── wsgi.py
答案 0 :(得分:1)
我想你错过了将pythonpath编写到你的django项目中。 您正在添加应用程序,而不是项目。
在testbot / settings.py中添加:
# Setting up django's project full path.
import sys
sys.path.insert(0, '/home/TristanTrim/PycharmProjects/testsite')
# Setting up django's settings module name.
# This module is located at /home/rolando/projects/myweb/myweb/settings.py.
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'testsite.settings'
答案 1 :(得分:0)
import sys
import os
#设置django项目的完整路径。
sys.path.append('/home/testsite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'testsite.settings'