2009-11-27 55 views
2

我有以下腳本我使用從我的UNI網站報廢數據並插入到一個GAE Db的python腳本優化的應用程序引擎

from mechanize import Browser 
from BeautifulSoup import BeautifulSoup 
import re 
import datetime 

__author__ = "Nash Rafeeq" 

url = "http://webspace.apiit.edu.my/schedule/timetable.jsp" 
viewurl = "http://localhost:8000/timekeeper/intake/checkintake/" 
inserturl = "http://localhost:8000/timekeeper/intake/addintake/" 
print url 
mech = Browser() 
try: 
    page = mech.open(url) 
    html = page.read() 
except Exception, err: 
    print str(err) 
#print html 
soup = BeautifulSoup(html) 
soup.prettify() 
tables = soup.find('select') 
for options in tables: 
    intake = options.string 
    #print intake 
    try: 
     #print viewurl+intake 
     page = mech.open(viewurl+intake) 
     html = page.read() 
     print html 
     if html=="Exist in database": 
      print intake, " Exist in the database skiping" 
     else: 
      page = mech.open(inserturl+intake) 
      html = page.read() 
      print html 
      if html=="Ok": 
       print intake, "added to the database" 
      else: 
       print "Error adding ", intake, " to database" 
    except Exception, err: 
     print str(err) 

我想知道什麼是優化這個腳本的最佳方式所以我可以在應用引擎服務器上運行它。因爲它是,它現在刮超過300項,並採取超過10分鐘插入我的本地機器上的所有數據

正被用來存儲數據的模型是

class Intake(db.Model): 
    intake=db.StringProperty(multiline=False, required=True) 
    #@permerlink  
    def get_absolute_url(self): 
     return "/timekeeper/%s/" % self.intake 
    class Meta: 
     db_table = "Intake" 
     verbose_name_plural = "Intakes" 
     ordering = ['intake'] 

回答

4

Divide and conquer

  1. 做任務的列表(例如URL湊/分析)
  2. 添加您的任務到一個隊列(appengine taskqueue apiamazon sqs,...)
  3. 處理您的隊列
+0

+1爲分而治之策略:不能錯過! – jldupont 2009-11-27 15:37:48

+0

它只能從一個網址中獲取。被刮掉的是超過300個條目的選項列表。所以如果我把它分成任務它將成爲兩項任務。我猜測一個是刮,另一個是插入。這是否會達到配額限制?或者是他們另一種優化的方式。總的noob在這裏,所以請忍受我 – 2009-11-27 15:41:41

+0

在你粘貼的代碼中,你正在做一個for循環內的重複請求 - 所以它不只是一個獲取。 – 2009-11-27 15:47:41

2

的第一件事你應該做的是重寫腳本直接使用App Engine數據存儲。您花費的大部分時間毫無疑問是因爲您正在使用HTTP請求(每個條目兩個!)將數據插入數據存儲區。直接使用數據存儲區batch puts應該在運行時間內減少幾個數量級。

如果您的解析代碼仍然太慢,可以將工作分解爲塊,然後使用task queue API在多個請求中執行該工作。根據

1

喜託什和尼克我已經修改了腳本波紋管

from google.appengine.api import urlfetch 
from BeautifulSoup import BeautifulSoup 
from timkeeper.models import Intake 
from google.appengine.ext import db 

__author__ = "Nash Rafeeq" 

url = "http://webspace.apiit.edu.my/schedule/timetable.jsp" 
try: 
    page = urlfetch.fetch(url) 
    #print html 
    soup = BeautifulSoup(page.content) 
    soup.prettify() 
    tables = soup.find('select') 
    models=[] 
    for options in tables: 
     intake_code = options.string 
     if Intake.all().filter('intake',intake_code).count()<1: 
      data = Intake(intake=intake_code) 
      models.append(data) 
    try: 
     if len(models)>0: 
      db.put(models) 
     else: 
      pass 
    except Exception,err: 
     pass 
except Exception, err: 
    print str(err) 

我在正確的軌道上?我也不確定如何按計劃調用(每週一次),最好的方法是做什麼?

並感謝您的及時答案

+0

你可能想看看應用引擎的cron服務。 http://code.google.com/appengine/docs/python/config/cron.html – tosh 2009-11-27 17:35:42