在scraperwiki CPU tim上保存和恢复

2024-06-16 09:56:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次这么做,所以我最好提前为我的菜鸟错误道歉。我想刮胡子legacy.com网站对于第一页,搜索状态中的名字和姓氏得到的结果。我是编程新手,用scraperwiki编写代码。它起作用了,但在10000个ish查询还没来得及处理之前,我就耗尽了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

Tags: fromimportcomiddatalegacylinksfname
1条回答
网友
1楼 · 发布于 2024-06-16 09:56:13

在CSV循环的底部,用save_var编写每个fname+lname+state组合。然后,在该循环之前,添加另一个循环,该循环遍历行而不处理它们,直到它传递保存的值为止。在

您应该能够将整个web页面写入数据存储,但我还没有测试过。在

相关问题 更多 >