使用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實現(xiàn)敲木魚加功德包含加音效和敲擊動作(附demo)
敲木魚加功德是一款很火的動畫,本文主要介紹了python實現(xiàn)敲木魚加功德包含加音效和敲擊動作,具有一定的參考價值,感興趣的可以了解一下2023-11-11django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫
這篇文章主要給大家介紹了關于django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用django具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08python,Java,JavaScript實現(xiàn)indexOf
這篇文章主要介紹了python,Java,JavaScript如何實現(xiàn)indexOf,幫助大家更好的理解indexOf,感興趣的朋友可以了解下2020-09-09