python3程序下载_从Python中下载文件3

news/2024/7/7 10:00:58

如果你想获取一个网页的内容到一个变量,只是读取

urllib.request.urlopen的响应:

import urllib.request

...

url = 'http://example.com/'

response = urllib.request.urlopen(url)

data = response.read() # a `bytes` object

text = data.decode('utf-8') # a `str`; this step can't be used if data is binary

import urllib.request

...

# Download the file from `url` and save it locally under `file_name`:

urllib.request.urlretrieve(url, file_name)

import urllib.request

...

# Download the file from `url`, save it in a temporary directory and get the

# path to it (e.g. '/tmp/tmpb48zma.txt') in the `file_name` variable:

file_name, headers = urllib.request.urlretrieve(url)

但请记住urlretrieve被认为是legacy,可能会被弃用(不知道为什么,虽然)。

因此,最正确的方法是使用urllib.request.urlopen函数返回一个类似文件的对象,它代表一个HTTP响应,并使用shutil.copyfileobj将它复制到一个真实文件。

import urllib.request

import shutil

...

# Download the file from `url` and save it locally under `file_name`:

with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:

shutil.copyfileobj(response, out_file)

如果这看起来太复杂,你可能想要更简单,将整个下载内容存储在一个字节对象中,然后将其写入文件。但这只适用于小文件。

import urllib.request

...

# Download the file from `url` and save it locally under `file_name`:

with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:

data = response.read() # a `bytes` object

out_file.write(data)

可以在运行中提取.gz(以及可能是其他格式)压缩数据,但是这样的操作可能需要HTTP服务器来支持对文件的随机访问。

import urllib.request

import gzip

...

# Read the first 64 bytes of the file inside the .gz archive located at `url`

url = 'http://example.com/something.gz'

with urllib.request.urlopen(url) as response:

with gzip.GzipFile(fileobj=response) as uncompressed:

file_header = uncompressed.read(64) # a `bytes` object

# Or do anything shown above using `uncompressed` instead of `response`.


http://www.niftyadmin.cn/n/4151900.html

相关文章

linux的shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

Shell脚本介绍 shell是一种脚本语言可以使用逻辑判断、循环等语法可以自定义函数shell是系统命令的集合shell脚本可以实现自动化运维,能大大增加我们的运维效率Shell脚本结构和执行 开头需要加#!/bin/bash以#开头的行作为解释说明脚本的名字以.sh结尾,用…

14 Java do while循环语句

14.1 语法 初始语句; do{循环体;条件控制语句; }while(条件判断语句); 14.2 实验 package demo;public class Demo7 {public static void main(String[] args) {int i 0;do {System.out.println("hello");i; }while (i<5);} }

mysql查询缓存到redis_php查询mysql并缓存到redis

首先安装redis&#xff0c;并在php环境中开启php_redis扩展。下面不多说了&#xff0c;直接上代码$redis new redis();$redis->connect(127.0.0.1, 6379);$blog $redis->get(redisrow);//如果$blog数组为空&#xff0c;则去数据库中查询&#xff0c;并加入到redis中if(…

2018年2月5日公益活动—微软power bi desktop逆天神器介绍

在当前互联网&#xff0c;由于大数据研究热潮&#xff0c;以及数据挖掘&#xff0c;机器学习等技术的改进&#xff0c;各种数据可视化图表层出不穷&#xff0c;如何让大数据生动呈现。本次微软最有价值专家公益活动&#xff0c;宋老师免费给数据可视化爱好者介绍一款逆天神器微…

python获取子进程返回值_Python 从subprocess运行的子进程中实时获取输出的例子 Python如何抓取程序的输出?...

关于python中用subprocess调用exe子进程的问题不懂我的人有什么资格对我指指点点&#xff0c;不了解我的人凭什么对我说三道四的。python杀死子进程后继续执行后面程序程序a(python写成)执行过程中调用程序b(fortran写成)&#xff0e;程序b在有些条os.system的返回值是运行结果…

setuptools python2.7_centos 6.5安装python2.7.10和setuptools和pip

如果你使用yum安装&#xff0c;那么源里的python 基本不会超过2.7目前官方最新的版本是2.7.10 (也许你看到该文章时已经不止了)所以我们要手工编译安装。 编译安装python比其他 方便多了。很可爱。1、确认你的centos有编译的环境以及工具如gcc.如果完全不知道这些&#xff0c;那…

企业分布式微服务云SpringCloud SpringBoot mybatis (一)服务的注册与发现(Eureka)...

一、spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具&#xff0c;包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单&#xff0c;可以在开发人员的电脑上跑。另外说明spring cloud是基…

在 Golang 中针对 int64 类型优化 abs()

原文&#xff1a;Optimized abs() for int64 in Go&#xff0c;译文&#xff1a;在 Golang 中针对 int64 类型优化 abs()&#xff0c;欢迎转载。 前言 Go 语言没有内置 abs() 标准函数来计算整数的绝对值&#xff0c;这里的绝对值是指负数、正数的非负表示。 我最近为了解决 Ad…