使用Python下載Bing圖片(代碼)
更新時間:2013年11月07日 09:05:41 作者:
代碼另存為py文件,運(yùn)行圖片將自動下載到py文件相同目錄,如果覺得每次運(yùn)行找文件麻煩,可以新建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>
復(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)文章
python實(shí)現(xiàn)敲木魚加功德包含加音效和敲擊動作(附demo)
敲木魚加功德是一款很火的動畫,本文主要介紹了python實(shí)現(xiàn)敲木魚加功德包含加音效和敲擊動作,具有一定的參考價值,感興趣的可以了解一下2023-11-11django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫
這篇文章主要給大家介紹了關(guān)于django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08python,Java,JavaScript實(shí)現(xiàn)indexOf
這篇文章主要介紹了python,Java,JavaScript如何實(shí)現(xiàn)indexOf,幫助大家更好的理解indexOf,感興趣的朋友可以了解下2020-09-09Python3對稱加密算法AES、DES3實(shí)例詳解
這篇文章主要介紹了Python3對稱加密算法AES、DES3,結(jié)合實(shí)例形式詳細(xì)分析了對稱加密算法AES、DES3相關(guān)模塊安裝、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-12-12