使用Python下载Bing图片(代码)

yipeiwu_com6年前Python基础
直接上代码:
复制代码 代码如下:

<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中的切片赋值

本文主要介绍的关于Python切片赋值的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 昨天有同学问了我这么个问题: t = [1, 2, 3] t[1:1] = [...

python实现数通设备端口监控示例

最近因工作需要,上面要求,每天需上报运维的几百数通设备端口使用情况【】,虽然有现成网管监控工具监控设备状态,但做报表,有点不方便,特写了个小脚本。注:测试运行于ubuntn,需安装snm...

Python3基础之输入和输出实例分析

通常来说,一个Python程序可以从键盘读取输入,也可以从文件读取输入;而程序的结果可以输出到屏幕上,也可以保存到文件中便于以后使用。本文就来介绍Python中最基本的I/O函数。 一、...

Python计算时间间隔(精确到微妙)的代码实例

Python计算时间间隔(精确到微妙)的代码实例

使用python中的datetime import datetime oldtime=datetime.datetime.now() print oldtime; x=1 while...

Python面向对象总结及类与正则表达式详解

Python面向对象总结及类与正则表达式详解

Python3 面向对象 --------------------------------------------------------------------------------...