python獲取網(wǎng)絡(luò)圖片方法及整理過程詳解
這篇文章主要介紹了python獲取網(wǎng)絡(luò)圖片方法及整理過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
方式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,使用系統(tǒng)庫文件讀寫操作
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)
網(wǎng)上的方法
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")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python利用PyVista進行mesh的色彩映射的實現(xiàn)
- Python Shiny庫創(chuàng)建交互式Web應(yīng)用及高級功能案例
- Python?Streamlit制作交互式可視化網(wǎng)頁應(yīng)用實例
- Gradio構(gòu)建交互式Python應(yīng)用使用示例詳解
- Python基于Google?Bard實現(xiàn)交互式聊天機器人
- Python深度學(xué)習(xí)pytorch神經(jīng)網(wǎng)絡(luò)圖像卷積運算詳解
- 使用Python的networkx繪制精美網(wǎng)絡(luò)圖教程
- Python Pyvis庫創(chuàng)建交互式網(wǎng)絡(luò)圖實例探究
相關(guān)文章
python except異常處理之后不退出,解決異常繼續(xù)執(zhí)行的實現(xiàn)
這篇文章主要介紹了python except異常處理之后不退出,解決異常繼續(xù)執(zhí)行的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
詳解pandas.DataFrame.plot() 畫圖函數(shù)
這篇文章主要介紹了詳解pandas.DataFrame.plot()畫圖函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
python3調(diào)用ansible?api使用實例例說明
這篇文章主要為大家介紹了python3?調(diào)用ansible?api使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
淺談pandas中Dataframe的查詢方法([], loc, iloc, at, iat, ix)
下面小編就為大家分享一篇淺談pandas中Dataframe的查詢方法([], loc, iloc, at, iat, ix),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
玩數(shù)據(jù)必備Python庫之numpy使用詳解
NumPy提供了許多高級的數(shù)值編程工具,如矩陣數(shù)據(jù)類型、矢量處理,以及精密的運算庫,下面這篇文章主要給大家介紹了關(guān)于玩數(shù)據(jù)必備Python庫之numpy使用的相關(guān)資料,需要的朋友可以參考下2022-02-02
python的mysql數(shù)據(jù)庫建立表與插入數(shù)據(jù)操作示例
這篇文章主要介紹了python的mysql數(shù)據(jù)庫建立表與插入數(shù)據(jù)操作,結(jié)合實例形式分析了python操作mysql數(shù)據(jù)庫建立表與插入數(shù)據(jù)相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-09-09

