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

复制代码 代码如下:#coding:utf-8 import urllib.request import xml.dom.minidom import sqlite3 import th...

详解Python 中sys.stdin.readline()的用法

之前在Python中输入都是用的input(),但是看到大家都用sys.stdin.readline(),没办法那我也得用. python3中使用sys.stdin.readline()...

python中numpy.zeros(np.zeros)的使用方法

翻译: 用法:zeros(shape, dtype=float, order='C') 返回:返回来一个给定形状和类型的用0填充的数组; 参数:shape:形状 dtype:数据类型,可...

Python运行DLL文件的方法

什么是DLL文件? DLL文件为动态链接库(英语: Dynamic-link library, 缩写为DLL) 它是微软公司在微软视窗操作系统中实现共享函数库概念的一种实现方式 先来...

Django模板语言 Tags使用详解

Tags # 普通for循环 <ul> {% for user in user_list %} <li>{{ user.name }}</li&...