使用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计算斗牛游戏概率算法。分享给大家供大家参考,具体如下: 过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛。在这些游戏中,斗牛是最受欢迎的,因为可...

Linux 发邮件磁盘空间监控(python)

核心代码: #!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib import os import commands,...

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

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

简单的python后台管理程序

简单的python后台管理程序

一、作业需求   二、流程图 三、源码与具体思路 import shutil import os import sys USER_LOGIN = {'is_lo...

python批量修改文件名的实现代码

#coding:utf-8 #批量修改文件名 import os import re import datetime re_st = r'(\d+)\+\s?\((...