2013-07-23 51 views
0

我有一個腳本在谷歌Appengine每20分鐘由cron.yaml啓動。這適用於本地,在我自己的機器上。當我(手動)到網上啓動腳本的網址時,它也可以工作。但是,當cron.yaml負責啓動它時,腳本總是無法在線完成,在Google的實例中。cron作業失敗gae python

日誌顯示沒有錯誤,只有2調試消息:

D 2013-07-23 06:00:08.449 
type(soup): <class 'bs4.BeautifulSoup'> END type(soup) 

D 2013-07-23 06:00:11.246 
type(soup): <class 'bs4.BeautifulSoup'> END type(soup) 

這裏是我的腳本:

# coding: utf-8 
import jinja2, webapp2, urllib2, re 

from bs4 import BeautifulSoup as bs 
from google.appengine.api import memcache 
from google.appengine.ext import db 

class Article(db.Model): 
    content = db.TextProperty() 
    datetime = db.DateTimeProperty(auto_now_add=True) 
    companies = db.ListProperty(db.Key) 
    url = db.StringProperty() 

class Company(db.Model): 
    name = db.StringProperty() 
    ticker = db.StringProperty() 

    @property 
    def articles(self): 
     return Article.gql("WHERE companies = :1", self.key()) 

def companies_key(companies_name=None): 
    return db.Key.from_path('Companies', companies_name or 'default_companies') 

def articles_key(articles_name=None): 
    return db.Key.from_path('Articles', articles_name or 'default_articles') 

def scrape(): 
    companies = memcache.get("companies") 
    if not companies: 
     companies = Company.all() 
     memcache.add("companies",companies,30) 
    for company in companies: 
     links = links(company.ticker) 
     links = set(links) 
     for link in links: 
      if link is not "None": 
       article_object = Article() 
       text = fetch(link)    
       article_object.content = text 
       article_object.url = link 
       article_object.companies.append(company.key()) #doesn't work. 
       article_object.put() 

def fetch(link): 
    try: 
     html = urllib2.urlopen(url).read() 
     soup = bs(html) 
    except: 
     return "None" 
    text = soup.get_text() 
    text = text.encode('utf-8') 
    text = text.decode('utf-8') 
    text = unicode(text) 
    if text is not "None": 
     return text 
    else: 
     return "None" 


def links(ticker): 
    url = "https://www.google.com/finance/company_news?q=NASDAQ:" + ticker + "&start=10&num=10" 
    html = urllib2.urlopen(url).read() 
    soup = bs(html) 
    div_class = re.compile("^g-section.*") 
    divs = soup.find_all("div", {"class" : div_class}) 
    links = [] 
    for div in divs: 
     a = unicode(div.find('a', attrs={'href': re.compile("^http://")})) 
     link_regex = re.search("(http://.*?)\"",a) 
     try: 
      link = link_regex.group(1) 
      soup = bs(link) 
      link = soup.get_text() 
     except: 
      link = "None" 
     links.append(link) 

    return links 

...和腳本的處理程序主:

class ScrapeHandler(webapp2.RequestHandler): 
    def get(self): 
     scrape.scrape() 
     self.redirect("/") 

我的猜測是這個問題可能是scrape腳本中的double for循環,但我不明白爲什麼。

更新: 文章確實被刮(應該有多少),現在沒有日誌錯誤,甚至根本沒有調試信息。看着日誌,cron作業看起來完美無瑕。即便如此,Appengine的cron工作小組表示cron工作失敗。

+0

發佈您的代碼,可能是它錯了。這些調試消息是否打印在cron作業處理程序中? – marcadian

+0

調試消息在線顯示在gae日誌中。這隻能在谷歌自己的雲中失敗。在我的機器上,它沒有任何警告或調試。 – memius

+0

你知道它沒有啓動,日誌表明有一些東西正在啓動。但可能沒有完成。我建議你在那裏放更多的日誌,看看進程到底有多遠。 –

回答

0

我很確定這個錯誤是由於DeadlineExceededError,我沒有在本地運行。我的scrape()腳本現在可以在較少的公司和文章上完成它的工作,並且不會超出最後期限。