나는 각 레코드 20 개씩의 테이블에서 약 400,000 개의 레코드를 긁는 프로젝트 작업을하고 있습니다. 현재 스크립트는 페이지의 전체 URL 목록을 작성한 다음 각 URL에 대해 페이지를 열고 BeautifulSoup로 표를 찾고 각 행을 스크랩합니다. 그것은 각 행을 긁는 것처럼 그것은 CSV에 행을 기록 : 나는 각 행을 쓰는 한 번에 대신 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)
. 아니면 더 많은 RAM을 사용하고 나머지 컴퓨터 속도를 느리게합니까? 이것을보다 효율적으로 만드는 다른 방법은?
파일을 열어 두어도 추가 모드로 열 필요는 없습니다. – martineau
@martineau 좋은 지적. 비록 내가 알파벳의 덩어리 위에 그것을 실행하고 있었으므로, 추가 할 필요가있다. – Amanda