方式1

使用urllib库

import urllib.request
import os ,stat
url = "https://cn.bing.com/th?id=OHR.Lidong2019_ZH-CN0761273672_1920x1080.jpg"
try:
urllib.request.urlretrieve(url,filename="/home/baixiaoxu/desk/123.jpg")
except IOError as e:
print("IOE ERROR")
except Exception as e:
print("Exception") 注意:
1,获取地址,判断地址是否存在
2,本地保存地址,判断存在
3,获取远程地址的图片名,或改名
"""
url = "https://cn.bing.com/th?id=OHR.Lidong2019_ZH-CN0761273672_1920x1080.jpg"
file_suffix = os.path.split(url)[1][-20:-1]
print(file_suffix)
"""

2,使用系统库文件读写操作

import urllib.request
import os ,stat req = urllib.request.Request(url)
file = "/home/baixiaoxu/desk/file-ttttt.jpg"
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0')
response = urllib.request.urlopen(url)
html = response.read()
with open(file, 'wb') as f:
f.write(html)

网上的方法


import os
os.makedirs('./image/', exist_ok=True)
IMAGE_URL = "http://image.nationalgeographic.com.cn/2017/1122/20171122113404332.jpg" def urllib_download():
from urllib.request import urlretrieve
urlretrieve(IMAGE_URL, './image/img1.png') def request_download():
import requests
r = requests.get(IMAGE_URL)
with open('./image/img2.png', 'wb') as f:
f.write(r.content) def chunk_download():
import requests
r = requests.get(IMAGE_URL, stream=True)
with open('./image/img3.png', 'wb') as f:
for chunk in r.iter_content(chunk_size=32):
f.write(chunk)

整理简单的下载图片

import  urllib
from urllib import request
import re response = request.urlopen('https://cn.bing.com/')
html = response.read()
ht = html.decode()
pattern = r'bgLink(.*?\.jpg)'
compile_re = re.compile(pattern) hh = compile_re.findall(ht)
url = hh[0].split('/')[1] download = 'https://cn.bing.com/' + url
urllib.request.urlretrieve(download,filename="/home/baixiaoxu/desk/download.jpg")

python获取网络图片的更多相关文章

  1. Python下载网络图片方法汇总与实现

    本文介绍下载python下载网络图片的方法,包括通过图片url直接下载.通过re/beautifulSoup解析html下载以及对动态网页的处理等. ​ 很多人学习python,不知道从何学起.很多人 ...

  2. 使用shell/python获取hostname/fqdn释疑

    一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...

  3. python 获取日期

    转载   原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...

  4. URL转Drawable之 Android中获取网络图片的三种方法

    转载自: http://doinone.iteye.com/blog/1074283 Android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Applicatio ...

  5. python获取字母在字母表对应位置的几种方法及性能对比较

    python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...

  6. python获取文件大小

    python获取文件大小 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import os # 字节bytes转化kb\m\g def formatSiz ...

  7. python 获取一个列表有多少连续列表

    python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:

  8. iOS 获取网络图片的大小

    一直都在找关于获取网络图片的大小的方法, 今天找到了一个能解决的办法 ,如下 1, 导入框架 #import <ImageIO/ImageIO.h> 2. 使用此方法得到image的siz ...

  9. [python实用代码片段]python获取当前时间的前一天,前一周,前一个月

    python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...

随机推荐

  1. nginx 反向代理是url带后缀

    1.后端:http://10.253.149.2/tdsqlpcloud/index.php 2.配置文件 danjan01deiMac:nginx danjan01$ cat /usr/local/ ...

  2. 动态赋予java类的属性set与get值

    public class Contact { private String name; private String tel; private attrControl attrC=new attrCo ...

  3. 【模板】SPFA(不完全详解)

    一种最短路求法(个人觉得比DIJKSTRA好用) 用于有向图. 大概思路:从根节点开始,枚举每一个点,同时更新他们所联通的点的最短路径,如果路径被更新,则把这个点入队,一直重复这个操作直到队伍为空为止 ...

  4. cookie,session,token的理解

    Get  POST 区别异同点 淘宝token的 理解   过程算法 防止伪造请求  伪造相对难 简单发展史  登录的操作: 哪些人往自己的购物车中放商品,  也就是说我必须把每个人区分开,这就是一个 ...

  5. 定制ListView的界面(让列表中不仅有文字还有图片fruitImage.setImageResource(fruit.getImageId());)

    1.定义自己的实体类来作为ListView的适配类型如: public class Fruit { private String name;//水果的名字 private int imageId;// ...

  6. MySQL中自定义排序

    在开发时候,我们经常使用的是默认的排序规则,但在某些特殊情况下,通过指定顺序来进行排序 -- fileld自定义排序时,应该是非主键的,否则主键是无效 SELECT * FROM customer W ...

  7. Vue报错:Property or method "XXX" is not defined on the instance but referenced during render. Make sure that this property is reactive...

    在Vue中定义方法或者属性时,因为粗心疏忽可以能会报该错误 [Vue warn]: Property or method "search" is not defined on th ...

  8. python中同步、多线程、异步IO、多线程对IO密集型的影响

    目录 1.常见并发类型 2.同步版本 3.多线程 4.异步IO 5.多进程 6.总结 1.常见并发类型 I/ O密集型: 蓝色框表示程序执行工作的时间,红色框表示等待I/O操作完成的时间.此图没有按比 ...

  9. vue-transition实现加入购物车效果及其他动画效果实现

    vue提供了<transition></transition>和<transition-group></transition-group>实现元素状态的 ...

  10. MyBatis--把SQL带进Java

    简单来看软件服务的工作流程:用户端界面操作请求<---->本地处理|远程服务程序拦截转发请求<---->服务端逻辑功能实现<--MyBatis用在这里-->数据库. ...