| #! /Usr/bin/env python #-*-Coding: UTF-8 -*- From gevent import monkey Monkey. patch_all () From gevent. pool import Pool Import requests Import sys Import OS Def download (url ): Chrome = 'mozilla/5.0 (X11; Linux i86_64) AppleWebKit/123456' + '(KHTML, like Gecko) Chrome/41.0.2272.101 Safari/123' Headers = {'user-agent': chrome} Filename = url. split ('/') [-1]. strip () R = requests. get (url. strip (), headers = headers, stream = True) With open (filename, 'wb ') as f: For chunk in r. iter_content (chunk_size = 1024 ): If chunk: F. write (chunk) F. flush () Print filename, "is OK" Def removeLine (key, filename ): OS. system ('sed-I/% s/d % s' % (key, filename )) If _ name _ = "_ main __": If len (sys. argv) = 2: Filename = sys. argv [1] F = open (filename, "r ") P = Pool (4) For line in f. readlines (): If line: P. spawn (download, line. strip ()) Key = line. split ('/') [-1]. strip () RemoveLine (key, filename) F. close () P. join () Else: Print 'usage: python % s urls.txt '% sys. argv [0] |