python - 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
解决方案
它不起作用,因为您在脚本中使用模型之前没有初始化 Django。最好的方法 - 使用 django 命令。文档:https ://docs.djangoproject.com/en/3.0/howto/custom-management-commands/#module-django.core.management
推荐阅读
- angular - 模块“AppModule”导入的意外值“MatBottomSheet”。请添加@NgModule 注释
- opengl - 有什么方法可以在 Rust 中使用屏幕外渲染
- php - jquery - 更改输入时的raido按钮选择
- c# - 试图在两点之间徘徊,但得到 NullReferenceException [统一]
- angular - 单击下载时如何更改primeng中的导出csv名称?
- npm - 如何正确使用 npm 和 webpack 要求 js-cookie?未捕获的引用错误
- r - 这是什么错误;“(函数(文件名=“Rplot%03d.png”,宽度= 480,高度= 480,:无法启动png()设备)中的错误?
- c++ - 分段故障说明
- javascript - 在 NodeJS 中使用«Canvas»:«Cannot find module '../build/Release/canvas.node'»
- reactjs - 使用路由器调用兄弟组件函数