使用Python下載Bing圖片(代碼)
更新時間:2013年11月07日 09:05:41 作者:
代碼另存為py文件,運行圖片將自動下載到py文件相同目錄,如果覺得每次運行找文件麻煩,可以新建py文件的快捷方式,程序還會自動給下載的圖片命名
直接上代碼:
<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>
復制代碼 代碼如下:
<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>
相關文章
Python使用Beets模塊實現(xiàn)自動整理音樂庫
Beets是一個功能強大的Python庫,用于處理音樂文件的元數據,在本文中,我們將探討beets模塊的常見使用方法,感興趣的可以跟隨小編一起學習一下2024-03-03
Python中plt.imshow(image)無法顯示圖片的解決
這篇文章主要介紹了Python中plt.imshow(image)無法顯示圖片的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

