首页 > 解决方案 > 使用 Python Django 我怎么能做自定义函数,保存到 db 并返回 json 响应

问题描述

我创建了一个爬虫并希望将它与 API 集成。从 Python Django 开始非常好,并且提供了简单的开始,但是我在以下代码结构中遇到了问题:

在 /test 上获取请求

  1. 抓取数据
  2. 将数据保存到数据库
  3. 返回数据作为响应

基本上我无法将列表的数据保存在数据库中并将该数据作为响应返回。Scraper 将在 utils.py 中的另一个 python 文件中,api 将调用一个函数,它将返回已抓取数据的列表

有没有人有我想要达到的目标的例子?

标签: pythondjangopython-3.x

解决方案


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.


推荐阅读