0
我正在處理一個項目,每個項目在20個記錄的表中擦除約400,000條記錄。目前,我的腳本爲頁面創建了一個完整的URL列表,然後爲每個URL打開頁面,使用BeautifulSoup查找表格並擦除每一行。由於它刮掉每一行吧行寫入CSV:刮表和將行寫入CSV
def scrape_table(url):
soup = get_soup(url)
table = soup.find('table' , {'id' : 'BigTable'})
for row in table.find_all('tr'):
cells = row.find_all('td')
if len(cells) > 0:
name_bits = cells[0].get_text().strip().split(',')
first_name = name_bits[0].strip()
last_name = name_bits[1].strip()
species = cells[1].get_text().strip()
bunch = re.sub(u'[\xa0\xc2\s]+',' ',str(cells[5]),flags=re.UNICODE).strip()
bunch_strings = list(BeautifulSoup(bunch).td.strings)
weight = bunch_strings[1].strip()
bunch_match = re.match("dob:(.*) Mother: \$(.*)",bunch_strings[2].strip())
dob = date_format(bunch_match.groups()[0].strip())
mother = bunch_match.groups()[1].strip()
row_of_data = {
'first_name': first_name,
'last_name' : last_name,
'species' : species,
'weight' : weight,
'dob' : dob,
'mother' : mother
}
data_order = ['first_name', 'last_name', 'dob', 'mother', 'weight', 'kesavan']
csv_row(row_of_data,data_order,'elephants')
else:
continue
def csv_row(data,fieldorder,filename, base=__base__):
full_path = __base__+filename+'.csv'
print "writing", full_path
with open(full_path, 'a+') as csvfile:
linewriter = csv.DictWriter(csvfile, fieldorder, delimiter='|',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
linewriter.writerow(data)
我想知道如果,如果我寫的每一結果頁到CSV一次,而不是寫每一行的,這將是更有效的。或者是否會使用更多內存並減慢我的其他計算機?其他方法可以提高效率?
如果文件保持打開狀態,則可能無需在追加模式下打開該文件。 – martineau
@martineau好點。雖然我一直在跑遍它的字母大寫,所以我確實需要它來追加。 – Amanda