将Scrapy与Django集成:如何

时间:2018-06-01 07:09:35

标签: python django scrapy

我还是Django的新手 我正在关注如何整合scrapy和django的this教程。

问题是,当我尝试使用自己的蜘蛛时,它不会起作用。 我在django外面试过蜘蛛,它的工作很好,一些帮助会很有帮助。

这是我的spider.py文件

import scrapy
from scrapy_splash import SplashRequest

class NewsSpider(scrapy.Spider):
   name = 'detik'
   allowed_domains = ['news.detik.com']
   start_urls = ['https://news.detik.com/indeks/all/?date=02/28/2018']

def parse(self, response):  
    urls = response.xpath("//div/article/a/@href").extract()        
    for url in urls:
        url = response.urljoin(url)
        yield scrapy.Request(url=url, callback=self.parse_detail)

    # follow pagination link
    page_next =   response.xpath("//a[@class = 'last']/@href").extract_first()
    if page_next:
        page_next = response.urljoin(page_next)
        yield scrapy.Request(url=page_next, callback=self.parse)

def parse_detail(self,response):
    x = {}
    x['breadcrumbs'] = response.xpath("//div[@class='breadcrumb']/a/text()").extract(),
    x['tanggal'] = response.xpath("//div[@class='date']/text()").extract_first(),
    x['penulis'] = response.xpath("//div[@class='author']/text()").extract_first(),
    x['judul'] = response.xpath("//h1/text()").extract_first(),
    x['berita'] = response.xpath("normalize-space(//div[@class='detail_text'])").extract_first(),
    x['tag'] = response.xpath("//div[@class='detail_tag']/a/text()").extract(),
    x['url'] = response.request.url,
    return x

这是我的管道文件

class DetikAppPipeline(object):

def process_item(self, item, spider):
    item = detikNewsItem()
    self.items.append(item['breadcrumbs'])
    self.items.append(item['tanggal'])
    self.items.append(item['penulis'])
    self.items.append(item['judul'])
    self.items.append(item['berita'])
    self.items.append(item['tag'])
    self.items.append(item['url'])
    item.save()

这是django中的模型文件

class detikNewsItem(models.Model):
    breadcrumbs = models.TextField()
    tanggal = models.TextField()
    penulis = models.TextField()
    judul = models.TextField()
    berita = models.TextField()
    tag = models.TextField()
    url = models.TextField()

    @property
    def to_dict(self):
    data = {
        'url': json.loads(self.url),
        'tanggal': self.tanggal
    }
    return data

    def __str__(self):
        return self.url

1 个答案:

答案 0 :(得分:0)

这是如何在Django项目中编写Scrapy桩线的例子。

from <YOU_APP_NAME>.models import detikNewsItem

class DetikAppPipeline(object):
    def process_item(self, item, spider):
        d, created = detikNewsItem.objects.get_or_create(breadcrumbs=item['breadcrumbs'], url=item['url'])
        if created:        
            d.tanggal = item['tanggal']
            d.penulis = item['penulis']
            d.judul = item['judul']
            d.berita = item['berita']
            d.tag = item['tag']
            d.save()

        return item

顺便说一句,你需要在Django环境中运行Scrapy。有几种方法可以做到这一点:

1使用django-extensions模块。 需要创建新文件:

<DJANG_PROJECT>/scripts/__init__.py
<DJANG_PROJECT>/scripts/run_scrapy.py

内置代码:

from scrapy.cmdline import execute

execute(['run_scrapy.py', 'crawl', 'detik'])

2另一种方法是使用Django Managment。需要在项目中使用file:

创建文件夹
<folder_of_app>/management/commands/__init__.py
<folder_of_app>/management/commands/scrapy.py

scrapy.py文件应该包含代码:

从scrapy.cmdline导入执行 来自django.core.management.base导入BaseCommand

class Command(BaseCommand):
    help = 'Run scrapy.'

    def add_arguments(self, parser):
        parser.add_argument('arguments', nargs='+', type=str)

    def handle(self, *args, **options):
        args = []
        args.append('scrapy.py')
        args.extend(options['arguments'])
        execute(args)

它允许在Django环境中运行Scrapy,如下所示:

python manage.py scrapy crawl detik
python manage.py scrapy shell 'https://news.detik.com/indeks/all/?date=02/28/2018'