使用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 中使用 GLOBAL引发的一系列问题

哪里出问题了   python 中,使用 global 会将全局变量设为本函数可用。同时,在函数内部访问变量会先本地再全局。   在嵌套函数中,使用 global 会产生不合常理的行为。...

Python编程之字符串模板(Template)用法实例分析

Python编程之字符串模板(Template)用法实例分析

本文实例讲述了Python编程之字符串模板(Template)用法。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 字符串格式化操作符,需要程序员明确转换类型...

Python内置函数Type()函数一个有趣的用法

今天在网上看到type的一段代码 ,然后查了一下文档,才知道type还有三个参数的用法。 http://docs.python.org/2/library/functions.html...

使用Python函数进行模块化的实现

使用 Python 函数来最大程度地减少重复任务编码工作量。 你是否对函数、类、方法、库和模块等花哨的编程术语感到困惑?你是否在与变量作用域斗争? 无论你是自学成才的还是经过正式培训的程...

django的ORM模型的实现原理

ORM模型介绍 随着项目越来越大,采用写原生SQL的方式在代码中会出现大量的SQL语句,那么问题就出现了: SQL语句重复利用率不高,越复杂的SQL语句条件越多,代码越长。会出现...