2015-08-21 54 views
1

我嘗試運行在我的Docker容器中啓動許多蜘蛛的bash腳本。 我supervisor.conf是放在 「/etc/supervisor/conf.d/」 looke這樣的:已退出:scrapy(退出狀態0;未預期)

[program:scrapy]                
command=/tmp/start_spider.sh 
autorestart=false 
startretries=0 
stderr_logfile=/tmp/start_spider.err.log 
stdout_logfile=/tmp/start_spider.out.log 

但主管返回此錯誤:

2015年8月21日10:50:30466 CRIT監事以root身份運行(無用戶在 配置文件)

2015年8月21日10:50:30466解析

期間WARN包括額外的文件 「/etc/supervisor/conf.d/tor.conf」

2015年8月21日10:50:30478 INFO RPC接口 '主管' 初始化

2015年8月21日10:50:30478 CRIT服務器 'unix_http_server' 而不 運行任何HTTP認證檢查

2015 -08-21 10:50:30478 INFO supervisord開始使用PID 5

2015年8月21日10:50:31481 INFO催生: 'scrapy' 具有pid 8

2015年8月21日10時50分:31,555 INFO退出:scrapy(退出狀態0;不 預期)

2015年8月21日10:50:32557 INFO放棄:scrapy進入FATAL狀態,也 許多開始試太快

而我的程序停止運行。但如果我手動運行我的程序,它的工作原理非常好...

如何解決此問題?有任何想法嗎?

+0

什麼start_spider.sh看起來不一樣? – Michael

回答

2

我找到了解決我的問題的方法。對於supervisor.conf,改變

[program:scrapy]              
     command=/tmp/start_spider.sh 
     autorestart=false 
     startretries=0 

由:

[program:scrapy] 
command=/bin/bash -c "exec /tmp/start_spider.sh > /dev/null 2>&1 -DFOREGROUND" 
autostart=true 
autorestart=false 
startretries=0 
0

這裏是我的代碼:

start_spider.sh

#!/bin/bash 

# list letter 
parseLetter=('a' 'b') 


# change path 
cd $path/scrapy/scrapyTodo/scrapyTodo 

tLen=${#parseLetter[@]} 
for ((i=0; i<${tLen}; i++)); 
do 
    scrapy crawl root -a alpha=${parseLetter[$i]} & 
done 

這裏是我的scrapy代碼:

#!/usr/bin/python -tt 
# -*- coding: utf-8 -*- 

from scrapy.selector import Selector 
from elasticsearch import Elasticsearch 
from scrapy.contrib.spiders import CrawlSpider 
from scrapy.http import Request 
from urlparse import urljoin 
from bs4 import BeautifulSoup 
from scrapy.spider import BaseSpider 
from bs4 import BeautifulSoup 
from tools import sendEmail 
from tools import ElasticAction 
from tools import runlog 
from scrapy import signals 
from scrapy.xlib.pydispatch import dispatcher 
from datetime import datetime 
import re 

class studentCrawler(BaseSpider): 
    # Crawling Start 
    CrawlSpider.started_on = datetime.now() 

    name = "root" 


    DOWNLOAD_DELAY = 0 

    allowed_domains = ['website.com'] 

    ES_Index = "website" 
    ES_Type = "root" 
    ES_Ip = "127.0.0.1" 

    child_type = "level1" 

    handle_httpstatus_list = [404, 302, 503, 999, 200] #add any other code you need 

    es = ElasticAction(ES_Index, ES_Type, ES_Ip) 

    # Init 
    def __init__(self, alpha=''): 

     base_domain = 'https://www.website.com/directory/student-' + str(alpha) + "/" 

     self.start_urls = [base_domain] 
     super(CompanyCrawler, self).__init__(self.start_urls) 


    def is_empty(self, any_structure): 
     """ 
     Function that allow to check if the data is empty or not 
     :arg any_structure: any data 
     """ 
     if any_structure: 
      return 1 
     else: 
      return 0 

    def parse(self, response): 
     """ 
     main method that parse the web page 
     :param response: 
     :return: 
     """ 

     if response.status == 404: 
      self.es.insertIntoES(response.url, "False") 
     if str(response.status) == "503": 
      self.es.insertIntoES(response.url, "False") 
     if response.status == 999: 
      self.es.insertIntoES(response.url, "False") 

     if str(response.status) == "200": 
      # Selector 
      sel = Selector(response) 

      self.es.insertIntoES(response.url, "True") 
      body = self.getAllTheUrl('u'.join(sel.xpath(".//*[@id='seo-dir']/div/div[3]").extract()).strip(),response.url) 


    def getAllTheUrl(self, data, parent_id): 
     dictCompany = dict() 
     soup = BeautifulSoup(data,'html.parser') 
     for a in soup.find_all('a', href=True): 
      self.es.insertChildAndParent(self.child_type, str(a['href']), "False", parent_id) 

我發現BeautifulSoup不工作當蜘蛛被監督者啓動。 ...