使用Python下載Bing圖片(代碼)
更新時(shí)間:2013年11月07日 09:05:41 作者:
代碼另存為py文件,運(yùn)行圖片將自動(dòng)下載到py文件相同目錄,如果覺得每次運(yùn)行找文件麻煩,可以新建py文件的快捷方式,程序還會(huì)自動(dòng)給下載的圖片命名
直接上代碼:
<span style="font-family: arial,helvetica,sans-serif; font-size: 16px;"># -*- coding: cp936 -*-
import urllib
import os
print 'Download data......'
url = 'http://cn.bing.com'
urlFile = urllib.urlopen(url)
data = urlFile.read()
urlFile.close()
data = data.decode('utf-8')
pre = 'g_img={url:\''
index1 = data.find(pre) + len(pre)
index2 = data.find('\'', index1)
imgUrl = data[index1 : index2]
preImg = u'h3>今日圖片故事</h3><a href='
index3 = data.find(preImg) + len(preImg)
index4 = data.find('>', index3) + 1
index5 = data.find('<', index4)
imgName = data[index4 : index5] +u'.jpg'
if os.path.exists(imgName) == False:
print 'Download image......'
urllib.urlretrieve(imgUrl, imgName)
print 'Download complete'
os.startfile(imgName)
</span>
復(fù)制代碼 代碼如下:
<span style="font-family: arial,helvetica,sans-serif; font-size: 16px;"># -*- coding: cp936 -*-
import urllib
import os
print 'Download data......'
url = 'http://cn.bing.com'
urlFile = urllib.urlopen(url)
data = urlFile.read()
urlFile.close()
data = data.decode('utf-8')
pre = 'g_img={url:\''
index1 = data.find(pre) + len(pre)
index2 = data.find('\'', index1)
imgUrl = data[index1 : index2]
preImg = u'h3>今日圖片故事</h3><a href='
index3 = data.find(preImg) + len(preImg)
index4 = data.find('>', index3) + 1
index5 = data.find('<', index4)
imgName = data[index4 : index5] +u'.jpg'
if os.path.exists(imgName) == False:
print 'Download image......'
urllib.urlretrieve(imgUrl, imgName)
print 'Download complete'
os.startfile(imgName)
</span>
相關(guān)文章
解決python3 json數(shù)據(jù)包含中文的讀寫問題
這篇文章主要介紹了解決python3 json數(shù)據(jù)包含中文的讀寫問題,需要的朋友可以參考下2021-05-05
Python使用Beets模塊實(shí)現(xiàn)自動(dòng)整理音樂庫
Beets是一個(gè)功能強(qiáng)大的Python庫,用于處理音樂文件的元數(shù)據(jù),在本文中,我們將探討beets模塊的常見使用方法,感興趣的可以跟隨小編一起學(xué)習(xí)一下2024-03-03
啥是佩奇?使用Python自動(dòng)繪畫小豬佩奇的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于啥是佩奇?使用Python自動(dòng)繪畫小豬佩奇的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
Python中plt.imshow(image)無法顯示圖片的解決
這篇文章主要介紹了Python中plt.imshow(image)無法顯示圖片的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Python復(fù)數(shù)屬性和方法運(yùn)算操作示例
這篇文章主要介紹了Python復(fù)數(shù)屬性和方法運(yùn)算操作,結(jié)合實(shí)例形式分析了Python復(fù)數(shù)運(yùn)算相關(guān)操作技巧,代碼注釋備有詳盡說明,需要的朋友可以參考下2017-07-07

