2012-06-19 39 views
0

這是我第一次這樣做,所以我最好爲我的菜鳥錯誤提前道歉。我試圖通過搜索狀態中的第一個和最後一個名字來爲legacy.com搜索首頁結果。我是新手編程,並使用scraperwiki來執行代碼。它很有效,但在10,000個ish查詢有時間處理之前,我耗盡了CPU時間。現在我試圖保存進度,在時間不足的時候趕上,然後從停止的地方恢復。保存和恢復scraperwiki - CPU時間

我無法保存工作,任何與其他部分的幫助也將不勝感激。到目前爲止,我只是抓住了鏈接,但是如果有一種方法可以保存鏈接頁面的主要內容,那也是非常有用的。

這裏是我的代碼:

import scraperwiki 

from urllib import urlopen 
from BeautifulSoup import BeautifulSoup 

f = open('/tmp/workfile', 'w') 
#read database, find last, start from there 

def searchname(fname, lname, id, stateid): 
    url = 'http://www.legacy.com/ns/obitfinder/obituary-search.aspx?daterange=Last1Yrs&firstname= %s &lastname= %s &countryid=1&stateid=%s&affiliateid=all' % (fname, lname, stateid) 
    obits=urlopen(url) 
    soup=BeautifulSoup(obits) 
    obits_links=soup.findAll("div", {"class":"obitName"}) 
    print obits_links 
    s = str(obits_links) 
    id2 = int(id) 
    f.write(s) 
    #save the database here 
    scraperwiki.sqlite.save(unique_keys=['id2'], data=['id2', 'fname', 'lname', 'state_id', 's']) 


# Import Data from CSV 
import scraperwiki 
data = scraperwiki.scrape("https://dl.dropbox.com/u/14390755/legacy.csv") 
import csv 
reader = csv.DictReader(data.splitlines()) 
for row in reader: 
    #scraperwiki.sqlite.save(unique_keys=['id'], 'fname', 'lname', 'state_id', data=row) 
    FNAME = str(row['fname']) 
    LNAME = str(row['lname']) 
    ID = str(row['id']) 
    STATE = str(row['state_id']) 
    print "Person: %s %s" % (FNAME,LNAME) 
    searchname(FNAME, LNAME, ID, STATE) 


f.close() 
f = open('/tmp/workfile', 'r') 
data = f.read() 
print data 
+0

Scraperwiki是一個可愛的概念,但它還沒有準備好黃金時段。我想說你的第一個錯誤就是選擇一家f'd-company競爭者作爲平臺。 – pguardiario

回答

1

在CSV循環的底部,寫各FNAME LNAME + +的狀態結合save_var。然後,在該循環之前,添加另一個遍歷行的循環,而不處理它們直到它傳遞保存的值。

您應該可以將整個網頁寫入數據存儲,但我沒有測試過。