python - 使用 Python Django 我怎么能做自定义函数,保存到 db 并返回 json 响应
问题描述
我创建了一个爬虫并希望将它与 API 集成。从 Python Django 开始非常好,并且提供了简单的开始,但是我在以下代码结构中遇到了问题:
在 /test 上获取请求
- 抓取数据
- 将数据保存到数据库
- 返回数据作为响应
基本上我无法将列表的数据保存在数据库中并将该数据作为响应返回。Scraper 将在 utils.py 中的另一个 python 文件中,api 将调用一个函数,它将返回已抓取数据的列表
有没有人有我想要达到的目标的例子?
解决方案
You might use a custom django-admin command. You might be able to access all of your models as if you were developing a view. And you can trigger your command with ./manage.py mycustomcommand
.
推荐阅读
- java - 如何制作一个所有行都可以用作键的java表?
- r - 循环变异R中的多列
- mysql - MySQL中交叉检查用户行的外键约束
- php - Laravel 多关系数据获取
- java - 管理器应用程序上的 Tomcat 8 403 访问被拒绝(使用 Docker 设置)
- reactjs - 迁移到 monorepo 结构后 Styled-jsx 打字稿错误。“DetailedHTMLProps”类型上不存在属性“jsx”
- java - 突然关闭时关闭系统创建的所有进程
- node.js - 当我打开项目文件夹时,vs 代码终端(cmd)不起作用
- r - R在2个数据帧中使用多个键相应地获取值
- seaborn - 翻转 Seaborn 热图的颜色条