方式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. subprocess.call(cmd, shell=True)

    1.使用方法 subprocess.call() 执行由参数提供的命令. 我们可以用数组作为参数运行命令,也可以用字符串作为参数运行命令(通过设置参数shell=True) 注意,参数shell默认为 ...

  2. Redundant Connection

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  3. 解决The total number of locks exceeds the lock table size错误

    参考:https://blog.csdn.net/weixin_40683253/article/details/80762583 mysql在进行大批量的数据操作时,会报“The total num ...

  4. mysql update join

    随手记录一下 UPDATE information f1 LEFT JOIN topic f2 ON f1.id = f2.id SET f1.img_url = f2.img_url

  5. sql server delete语句

    delete语句 --DELETE 语句用于删除表中的行 语法:delete from 表名称 where 列名称 = 值 --可以在不删除表的情况下删除所有的行.这意味着表的结构.属性和索引都是完整 ...

  6. 第三篇 jQuery操作DOM

    3-1 DOM页面文档 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  7. golang(3):strings和strconv使用 & 时间和日期类型 & 指针类型 & 流程控制 & 函数

    strings和strconv使用 . strings.HasPrefix(s string, prefix string) bool: // 判断字符串s是否以prefix开头 . . string ...

  8. Java基础(那些习以为常缺不知道原理的地方)

    一.基础 1.1 正确的使用equals方法 Object的equals方法容易抛空指针异常,应使用常量或确定有值的对象来调用 equals.如下代码 // 不能使用一个值为null的引用类型变量来调 ...

  9. 一个页面两个div(一个柱状图或者折线图一个饼图)

    需求是一个页面中两个图,一个饼图一个折线图,接口用的是一个接口,柱状图的图例要隐藏掉,X轴为月份,每月份都有两个数据,也就是图例是两个(进口和出口)的意思饼图需要显示最新月份数据,并且有一个下拉框可以 ...

  10. ELF文件格式理解

    ELF(Executable and Linking Format)是一种对象文件的格式,用于定义不同类型的对象文件(Object files)中都放了什么东西.以及都以什么样的格式去放这些东西.它自 ...