使用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>

相关文章

使用coverage统计python web项目代码覆盖率的方法详解

使用coverage统计python web项目代码覆盖率的方法详解

本文实例讲述了使用coverage统计python web项目代码覆盖率的方法。分享给大家供大家参考,具体如下: 在使用python+selenium过程中,有时候考虑代码覆盖率,所以专...

基于python生成器封装的协程类

自从python2.2提供了yield关键字之后,python的生成器的很大一部分用途就是可以用来构建协同程序,能够将函数挂起返回中间值并能从上次离开的地方继续执行。python2.5的...

详解Python2.x中对Unicode编码的使用

我确定有很多关于Unicode和Python的说明,但为了方便自己的理解使用,我还是打算再写一些关于它们的东西。   字节流 vs Unicode对象 我们先来用Python定...

复习Python中的字符串知识点

字符串 在 Python 中创建字符串对象非常容易。只要将所需的文本放入一对引号中,就完成了一个新字符串的创建(参见清单 1)。如果稍加思考的话,您可能会感到有些困惑。毕竟,有两类可以使...

解决Python3 控制台输出InsecureRequestWarning问题

解决Python3 控制台输出InsecureRequestWarning的问题 问题: 使用Python3 requests发送HTTPS请求,已经关闭认证(verify=False)...