Day17

前回の続き。

def download_document(doc_id, save_path):
    params = {'type': 1}
    doc = get_document(doc_id, params)
    if doc.status_code == 200:
        with open(save_path + doc_id + '.zip', 'wb') as f:
            for chunk in doc.iter_content(chunk_size=1024):
                f.write(chunk)


・文書をダウンロードする関数。paramsでtype:1を指定して、この上に書かれていたget_document関数にdoc_idとparamsを渡して、返ってきたstatus_codeが200つまりOKだったら、save_path+doc_id+.zipで構成されているファイルを開いて、doc.iter_content()でバイナリデータを1024バイトずつ数回に分けてファイルに書き込んでいる。chunk とは一塊の意味で、このwith からの文は何かにおいての定番の書き方らしい。

今日は以上。備忘録。