我有以下腳本我使用從我的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']
+1爲分而治之策略:不能錯過! – jldupont 2009-11-27 15:37:48
它只能從一個網址中獲取。被刮掉的是超過300個條目的選項列表。所以如果我把它分成任務它將成爲兩項任務。我猜測一個是刮,另一個是插入。這是否會達到配額限制?或者是他們另一種優化的方式。總的noob在這裏,所以請忍受我 – 2009-11-27 15:41:41
在你粘貼的代碼中,你正在做一個for循環內的重複請求 - 所以它不只是一個獲取。 – 2009-11-27 15:47:41