• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python实现批量下载文件

Python实现批量下载文件

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了python实现文件传输,python 下载文件,python按行读取文件,python读取txt文件,python运行py文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Python实现批量下载文件

#!/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]
</div>

其他网友的方法:

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地址')
</div>

以上便是本文给大家分享的全部内容了,小伙伴们可以测试下哪种方法效率更高呢。

</div>

您可能想查找下面的文章:

  • python实现下载文件的三种方法
  • Python实现文件复制删除
  • python实现搜索本地文件信息写入文件的方法
  • python实现搜索本地文件信息写入文件的方法
  • Python实现分割文件及合并文件的方法
  • Python实现分割文件及合并文件的方法
  • Python实现大文件排序的方法
  • python实现文件快照加密保护的方法
  • Python实现删除文件但保留指定文件
  • python实现批量改文件名称的方法

相关文章

  • Python使用Paramiko模块编写脚本进行远程服务器操作
  • python用Pygal如何生成漂亮的SVG图像详解
  • 在python的类中动态添加属性与生成对象
  • 详解Python 数据库 (sqlite3)应用
  • 利用python程序生成word和PDF文档的方法
  • 使用Python的web.py框架实现类似Django的ORM查询的教程
  • python实现提取百度搜索结果的方法
  • python通过urllib2获取带有中文参数url内容的方法
  • python设置windows桌面壁纸的实现代码
  • python的else子句使用指南

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python实现调用其他python脚本的方法
    • Python实现计算文件夹下.h和.cpp文件的总行数
    • Python文件操作,open读写文件,追加文本内容实例
    • python 算法 排序实现快速排序
    • 分析Python中设计模式之Decorator装饰器模式的要点
    • Python中类的定义、继承及使用对象实例详解
    • python通过apply使用元祖和列表调用函数实例
    • Python教程之全局变量用法
    • Python的shutil模块中文件的复制操作函数详解
    • python检测服务器是否正常

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有