python - 异步抓取并使用django celery和redis存储结果并存储我的正确方法是什么?

标签 python django queue rabbitmq django-celery

我一直试图了解我的问题是什么,当我试图刮用我在我的django应用程序中创建的函数。该功能转到一个网站收集数据并将其存储在我的数据库中。起初,我尝试使用rq和redis一段时间,但一直收到一条错误消息。所以有人认为我应该试着用芹菜,我做到了。但我现在明白了RQ和芹菜都不是问题所在。因为我得到了和以前一样的错误信息。我已经厌倦了导入它,但仍然收到错误消息,然后我想,如果我在tasks.py文件中有实际的函数,它可能会有影响,但没有。这是我试图在tasks.py中使用的函数。

import requests
from bs4 import BeautifulSoup
from src.blog.models import Post
import random
import re
from django.contrib.auth.models import User
import os

@app.tasks
def p_panties():
    def swappo():
        user_one = ' "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" '
        user_two = ' "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)" '
        user_thr = ' "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko" '
        user_for = ' "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0" '

        agent_list = [user_one, user_two, user_thr, user_for]
        a = random.choice(agent_list)
        return a

    headers = {
        "user-agent": swappo(),
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
        "accept-encoding": "gzip,deflate,sdch",
        "accept-language": "en-US,en;q=0.8",
    }

    pan_url = 'http://www.example.org'
    shtml = requests.get(pan_url, headers=headers)
    soup = BeautifulSoup(shtml.text, 'html5lib')
    video_row = soup.find_all('div', {'class': 'post-start'})
    name = 'pan videos'

    if os.getenv('_system_name') == 'OSX':
        author = User.objects.get(id=2)
    else:
        author = User.objects.get(id=3)

    def youtube_link(url):
        youtube_page = requests.get(url, headers=headers)
        soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
        video_row = soupdata.find_all('p')[0]
        entries = [{'text': div,
                    } for div in video_row]
        tubby = str(entries[0]['text'])
        urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tubby)
        cleaned_url = urls[0].replace('?&autoplay=1', '')
        return cleaned_url

    def yt_id(code):
        the_id = code
        youtube_id = the_id.replace('https://www.youtube.com/embed/', '')
        return youtube_id

    def strip_hd(hd, move):
        str = hd
        new_hd = str.replace(move, '')
        return new_hd

    entries = [{'href': div.a.get('href'),
                'text': strip_hd(strip_hd(div.h2.text, '– Official video HD'), '– Oficial video HD').lstrip(),
                'embed': youtube_link(div.a.get('href')), #embed
                'comments': strip_hd(strip_hd(div.h2.text, '– Official video HD'), '– Oficial video HD').lstrip(),
                'src': 'https://i.ytimg.com/vi/' + yt_id(youtube_link(div.a.get('href'))) + '/maxresdefault.jpg', #image
                'name': name,
                'url': div.a.get('href'),
                'author': author,
                'video': True

                } for div in video_row][:13]

    for entry in entries:
        post = Post()
        post.title = entry['text']
        title = post.title
        if not Post.objects.filter(title=title):
            post.title = entry['text']
            post.name = entry['name']
            post.url = entry['url']
            post.body = entry['comments']
            post.image_url = entry['src']
            post.video_path = entry['embed']
            post.author = entry['author']
            post.video = entry['video']
            post.status = 'draft'
            post.save()
            post.tags.add("video", "Musica")
    return entries

如果我运行
from tasks import *

我明白了
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ray/Desktop/myheroku/practice/tasks.py", line 5, in <module>
    from src.blog.models import Post
  File "/Users/ray/Desktop/myheroku/practice/src/blog/models.py", line 3, in <module>
    from taggit.managers import TaggableManager
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/taggit/managers.py", line 7, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
    class ContentType(models.Model):
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 160, in ContentType
    app_label = models.CharField(max_length=100)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
    super(CharField, self).__init__(*args, **kwargs)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

这是我用RQ和ReDIS得到的完全相同的回溯。我发现如果我像这样修改导入
import requests
from bs4 import BeautifulSoup
# from src.blog.models import Post
import random
import re
# from django.contrib.auth.models import User
import os

像这样修改我的函数
@app.task
def p_panties():
    def swappo():
        user_one = ' "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" '
        user_two = ' "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)" '
        user_thr = ' "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko" '
        user_for = ' "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0" '

        agent_list = [user_one, user_two, user_thr, user_for]
        a = random.choice(agent_list)
        return a

    headers = {
        "user-agent": swappo(),
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
        "accept-encoding": "gzip,deflate,sdch",
        "accept-language": "en-US,en;q=0.8",
    }

    pan_url = 'http://www.example.org'
    shtml = requests.get(pan_url, headers=headers)
    soup = BeautifulSoup(shtml.text, 'html5lib')
    video_row = soup.find_all('div', {'class': 'post-start'})
    name = 'pan videos'

    # if os.getenv('_system_name') == 'OSX':
    #     author = User.objects.get(id=2)
    # else:
    #     author = User.objects.get(id=3)

    def youtube_link(url):
        youtube_page = requests.get(url, headers=headers)
        soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
        video_row = soupdata.find_all('p')[0]
        entries = [{'text': div,
                    } for div in video_row]
        tubby = str(entries[0]['text'])
        urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tubby)
        cleaned_url = urls[0].replace('?&amp;autoplay=1', '')
        return cleaned_url

    def yt_id(code):
        the_id = code
        youtube_id = the_id.replace('https://www.youtube.com/embed/', '')
        return youtube_id

    def strip_hd(hd, move):
        str = hd
        new_hd = str.replace(move, '')
        return new_hd

    entries = [{'href': div.a.get('href'),
                'text': strip_hd(strip_hd(div.h2.text, '– Official video HD'), '– Oficial video HD').lstrip(),
                'embed': youtube_link(div.a.get('href')), #embed
                'comments': strip_hd(strip_hd(div.h2.text, '– Official video HD'), '– Oficial video HD').lstrip(),
                'src': 'https://i.ytimg.com/vi/' + yt_id(youtube_link(div.a.get('href'))) + '/maxresdefault.jpg', #image
                'name': name,
                'url': div.a.get('href'),
                # 'author': author,
                'video': True

                } for div in video_row][:13]
    #
    # for entry in entries:
    #     post = Post()
    #     post.title = entry['text']
    #     title = post.title
    #     if not Post.objects.filter(title=title):
    #         post.title = entry['text']
    #         post.name = entry['name']
    #         post.url = entry['url']
    #         post.body = entry['comments']
    #         post.image_url = entry['src']
    #         post.video_path = entry['embed']
    #         post.author = entry['author']
    #         post.video = entry['video']
    #         post.status = 'draft'
    #         post.save()
    #         post.tags.add("video", "Musica")
    return entries

它工作,因为这是我的输出
[2016-08-13 08:31:17,222: INFO/MainProcess] Received task: tasks.p_panties[e196c6bf-2b87-4bb2-ae11-452e3c41434f]
[2016-08-13 08:31:17,238: INFO/Worker-4] Starting new HTTP connection (1): www.example.org
[2016-08-13 08:31:17,582: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:18,314: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:18,870: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:19,476: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:20,089: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:20,711: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:21,218: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:21,727: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:22,372: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:22,785: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:23,375: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:23,983: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:24,396: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:25,003: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:25,621: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:26,029: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:26,446: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:27,261: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:27,671: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:28,082: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:28,694: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:29,311: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:29,922: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:30,535: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:31,154: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:31,765: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:32,387: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:32,992: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:33,611: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:34,030: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:34,635: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:35,041: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:35,659: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:36,278: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:36,886: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:37,496: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:37,913: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:38,564: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:39,143: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:39,754: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:40,409: INFO/Worker-4] Starting new HTTP connection (1): example.org
[2016-08-13 08:31:40,992: INFO/MainProcess] Task tasks.p_panties[e196c6bf-2b87-4bb2-ae11-452e3c41434f] succeeded in 23.767645187006565s: [{'src': 'https://i.ytimg.com/vi/3bU-AtShW7Y/maxresdefault.jpg', 'name': 'pan videos', 'url':...

似乎需要某种类型的授权才能与我的post模型交互。我只是不知道怎么做。我一直在网上寻找如何将数据收集并保存到数据库中的示例。奇怪的是,我一个也没有碰到过。我能读到的任何建议都会有很大的帮助。
编辑
我的文件结构
environ\
  |-src\
     |-blog\
        |-migrations\
        |-static\
        |-templates\
        |-templatetags\
        |-__init__.py
        |-admin.py
        |-forms.py
        |-models
        |-tasks
        |-urls
        |-views

最佳答案

你需要设置django
您似乎试图在python shell中运行任务,这更有可能是因为当您注释掉django模型部分时,您的代码可以工作。
所以问题是,当运行纯python shell时,需要设置django,才能正常运行。当您在manage.py shell中运行它时,manage.py会为您处理或设置它,但是通过python脚本执行它需要手动设置。这就是缺少django_settings_模块错误的原因。
您似乎还使用了定义的模型,为了能够将它们导入到python脚本中,您需要将项目根文件夹的路径添加到当前python路径中。
最后,您需要告诉django您的设置文件在哪里(在设置django之前),在manage.py文件中,您应该具有如下内容:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

将其设为常量,将其命名为默认设置模块,这样您现在可以:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", DEFAULT_SETTINGS_MODULE)

现在您需要将常量导入到脚本中,并告诉django(通过设置env var)它应该在哪里查找设置文件。
总之:
import sys, os
sys.path.insert(0, "/path/to/parent/of/src") # /home/projects/my-crawler

from manage import DEFAULT_SETTINGS_MODULE
os.environ.setdefault("DJANGO_SETTINGS_MODULE", DEFAULT_SETTINGS_MODULE)

import django
django.setup() 
... The rest of your script ...

这样你的设置就很好了。但是如果您想运行芹菜任务,您应该使用.delay().apply_async(),以确保代码在后台运行。
我自己的建议是使用python manage.py shell运行pythonshell,本例中django为您处理所有事情。你只需要导入你的任务并运行它。
关于抓取任务结果的存储,您可以在数据库中,或者在redis中,或者在任何地方(一个文件,另一个web服务器…等等,您还可以调用另一个芹菜任务来处理结果并将条目传递给它)。
只需将其添加到任务代码的末尾。
Redis公司
from redis import StrictRedis

redis = StrictRedis(host='localhost', port=6379, db=0)

redis.set("scraping:tasks:results:TASK-ID-HERE", json.dumps(entries))

这是保存结果的最简单方法,但也可以使用redislists/maps
仅供参考,这是你使用列表的方法
with redis.pipeline() as pipe:
    for item in entries:
        pipe.rpush("scraping:tasks:results", json.dumps(item))
    pipe.execute()

----编辑
正如我所提到的,您可以定义另一个芹菜任务来处理当前刮除的结果。所以基本上你有以下几点:
@celery_app.task
def handle_scraping_results(entries):
    you do whatever you want with the entries array now

在你的内裤任务结束时这样称呼它:
handle_scraping_results.delay(entries)

rabbitmq在这里所做的,是传递来自您的p_u内裤任务的消息,处理_u scraping_u results任务。需要注意的是,这些不是简单的函数,共享相同的内存地址空间,它们可以在不同的进程上,在不同的服务器上!其实芹菜是用来吃的。不能调用处于不同进程中的函数。RabByMQ来到这里,从进程A(具有任务PN内裤)获取消息,并将其传递给进程B(具有任务句柄结果)(消息传递是RPC的完美方法)。
你不能在rabbitmq中保存任何东西,它不像redis。我劝你多读一点celery,因为你似乎选错了。使用芹菜并不能解决你的问题,它实际上增加了你的问题(因为一开始可能很难理解)。如果你不需要异步处理,只需把芹菜完全除掉。让您的代码成为一个单独的函数,您可以很容易地从python shell或manage.py shell调用它,就像我上面描述的那样。
——编辑二
你想在数据库中每隔几个小时坚持一次。所以当你的任务在某个地方完成或者结果丢失时,你必须坚持。
你有两个选择
在任务完成时(不是每隔几个小时)在数据库中保持
每当你的任务完成,然后每隔几个小时左右,你就会有一个periodic task,把它们保存在django数据库中。
第一种方法很简单,只需在自己的代码中取消注释已注释的代码。
第二条路需要更多的工作。
考虑到您的结果像我告诉您的那样被持久化在redis中,您可以有如下的周期性任务来为您处理持久化到db。
redis_keys = redis.get("scraping:tasks:results:*")

for key in redis_keys:
    value_of_redis_key = redis.get(key)
    entries = json.loads(entries)
    for entry in entries:
        post = Post()
        post.title = entry['text']
        title = post.title
        if not Post.objects.filter(title=title):
            post.title = entry['text']
            post.name = entry['name']
            post.url = entry['url']
            post.body = entry['comments']
            post.image_url = entry['src']
            post.video_path = entry['embed']
            post.author = entry['author']
            post.video = entry['video']
            post.status = 'draft'
            post.save()
            post.tags.add("video", "Musica")

关于python - 异步抓取并使用django celery和redis存储结果并存储我的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38933035/

相关文章:

queue - 如何在Python中随机打乱asyncio.Queue?

python - SymPy - 替换卷积中乘法的傅里叶

python - 在 Python 环境中管理 Perl 习惯

python - 如何在 django 中为每个匿名用户提供一些唯一的 id

django - ModelChoiceField 始终引发 : "Select a valid choice. That choice is not one of the available choices"

c++11 - 从 boost lock free spsc 队列中得到错误的输出

python - 如何在python openCV中计算六边形的内角

django - 获取模板中的聚合

python - 如何在 Django 中保存一些数据而不将其添加到表单中?

queue - 在 O(1) 时间内支持最小、最大操作的队列的正确数据结构是什么?