Python實(shí)現(xiàn)批量下載文件
Python實(shí)現(xiàn)批量下載文件
#!/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/537.36 ' +
'(KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
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]
其他網(wǎng)友的方法:
from os.path import basename
from urlparse import urlsplit
def url2name(url):
return basename(urlsplit(url)[2])
def download(url, localFileName = None):
localName = url2name(url)
req = urllib2.Request(url)
r = urllib2.urlopen(req)
if r.info().has_key('Content-Disposition'):
# If the response has Content-Disposition, we take file name from it
localName = r.info()['Content-Disposition'].split('filename=')[1]
if localName[0] == '"' or localName[0] == "'":
localName = localName[1:-1]
elif r.url != url:
# if we were redirected, the real file name we take from the final URL
localName = url2name(r.url)
if localFileName:
# we can force to save the file as specified name
localName = localFileName
f = open(localName, 'wb')
f.write(r.read())
f.close()
download(r'你要下載的python文件的url地址')
以上便是本文給大家分享的全部?jī)?nèi)容了,小伙伴們可以測(cè)試下哪種方法效率更高呢。
- Python獲取遠(yuǎn)程文件大小的函數(shù)代碼分享
- python實(shí)現(xiàn)帶錯(cuò)誤處理功能的遠(yuǎn)程文件讀取方法
- python通過paramiko復(fù)制遠(yuǎn)程文件及文件目錄到本地
- python定時(shí)復(fù)制遠(yuǎn)程文件夾中所有文件
- python使用Paramiko模塊實(shí)現(xiàn)遠(yuǎn)程文件拷貝
- python 從遠(yuǎn)程服務(wù)器下載日志文件的程序
- Python通過paramiko遠(yuǎn)程下載Linux服務(wù)器上的文件實(shí)例
- python實(shí)現(xiàn)下載文件的三種方法
- Python實(shí)現(xiàn)HTTP協(xié)議下的文件下載方法總結(jié)
- Python實(shí)現(xiàn)的遠(yuǎn)程文件自動(dòng)打包并下載功能示例
相關(guān)文章
使用Python通過代碼創(chuàng)建圖表的詳細(xì)步驟
這篇文章主要介紹了使用Python通過代碼創(chuàng)建圖表的詳細(xì)步驟,文中介紹了如何使用DiagramasCode工具創(chuàng)建基礎(chǔ)架構(gòu)的架構(gòu)圖,并通過Python腳本生成并上傳到對(duì)象存儲(chǔ)桶,需要的朋友可以參考下2024-12-12
Python實(shí)現(xiàn)合并同一個(gè)文件夾下所有txt文件的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)合并同一個(gè)文件夾下所有txt文件的方法,涉及Python針對(duì)文件的遍歷、讀取、寫入等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
用Python進(jìn)行柵格數(shù)據(jù)的分區(qū)統(tǒng)計(jì)和批量提取
該教程其實(shí)源于web,我看到之后覺得很實(shí)用,于是自己又重復(fù)做了一遍,寫了詳細(xì)的注釋分享給大家,希望對(duì)大家的研究有幫助,本文講述了柵格的分區(qū)統(tǒng)計(jì),批量提取,深化理解遍歷循環(huán)等內(nèi)容2021-05-05
python 使用poster模塊進(jìn)行http方式的文件傳輸?shù)椒?wù)器的方法
今天小編就為大家分享一篇python 使用poster模塊進(jìn)行http方式的文件傳輸?shù)椒?wù)器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
PyQt中使用QProcess運(yùn)行一個(gè)進(jìn)程的示例代碼
這篇文章主要介紹了在PyQt中使用QProcess運(yùn)行一個(gè)進(jìn)程,本例中通過按下按鈕,啟動(dòng)了windows系統(tǒng)自帶的記事本程序,即notepad.exe, 因?yàn)樗趙indows的系統(tǒng)目錄下,該目錄已經(jīng)加在了系統(tǒng)的PATH環(huán)境變量中,所以不需要特別指定路徑,需要的朋友可以參考下2022-12-12
Python爬蟲之Selenium多窗口切換的實(shí)現(xiàn)
這篇文章主要介紹了Python爬蟲之Selenium多窗口切換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
手把手教你實(shí)現(xiàn)Python重試超時(shí)裝飾器
這篇文章主要為大家介紹了實(shí)現(xiàn)Python重試超時(shí)裝飾器教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2023-05-05

