首页 > 解决方案 > Using models in a scrape to access data

问题描述

I'm using Python (w/ Django) with BeautifulSoup to scrape a number of websites. I want to use a list of websites in a DB I have set up with my Django App to loop through some websites and grab some information

I have a "Webpages" file with "models.py". This all works and I have set up a site simply to show the list of webpages in the DB

I have then added a "Scrape.py" file and can successfully do a simple scrape by going to Git Bash and running "py scrape.py"

However, I want to loop through the websites in my DB. To do so I thought I would need to do this first and then use it to fetch data

from .webpages.models import Webpage

But before I add any further code I get the error "ImportError: attempted relative import with no known parent package"

I have also tried

from webpages.models import Webpage

But then I get the error "ModuleNotFoundError: No module named 'webpages'"

I have searched online and found this guide, and followed but no such luck. As guide it asks you to place the following code in the file

 print('__file__={0:<35} | __name__={1:<20} | __package__={2:<20}'.format(__file__,__name__,str(__package__)))

... and it becomes clear what the issue is - when I run the script there are large blanks where there should be information about the package the module belongs. No matter what I do I can't seem to get that information to register

My files are structured as such

 project
 ├── webpages
     ├── models.py
 └── scrapes
     ├── scrape.py

I'm trying to run the above through Git Bash, but also added -m but still no luck

标签: pythondjango

解决方案


它不起作用,因为您在脚本中使用模型之前没有初始化 Django。最好的方法 - 使用 django 命令。文档:https ://docs.djangoproject.com/en/3.0/howto/custom-management-commands/#module-django.core.management


推荐阅读