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

yipeiwu_com5年前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编程实现随机生成多个椭圆实例代码

椭圆演示: 代码示例: import matplotlib.pyplot as plt import numpy as np from matplotlib.patches imp...

Python实现字典(dict)的迭代操作示例

本文实例讲述了Python实现字典(dict)的迭代操作。分享给大家供大家参考,具体如下: #!/usr/bin/python # -*- coding:utf-8 -*- #! p...

Python列表list内建函数用法实例分析【insert、remove、index、pop等】

Python列表list内建函数用法实例分析【insert、remove、index、pop等】

本文实例讲述了Python列表list内建函数用法。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 标准类型函数: cmp():进行序列比较的算法规则如下:...

Python+Selenium自动化实现分页(pagination)处理

场景 对分页来说,我们最感兴趣的是下面几个信息 总共有多少页 当前是第几页 是否可以上一页和下一页 代码 下面代码演示如何获取分页总数及当前页数、跳转到指定页数 #coding:u...

Python I/O与进程的详细讲解

Python I/O与进程的详细讲解

I/O with语句 with context_expression [as target(s)]: with-body context_expression返回值遵从上下文...