获取bing背景图片

搜索引擎bing的界面非常简洁,背景图片大气漂亮,非常适合用做桌面背景。于是就参考网上的教程写来一段python脚本,自动抓取bing的背景图片。代码如下:

#!/usr/bin/env python
#-*-coding: utf-8 -*-

import urllib, re, os

def get_bing_wallpaper():
    url = "http://cn.bing.com/"
    html = urllib.urlopen(url).read()
    if not html:
        print 'open & read bing error'
        return -1

    reg = re.compile(';g_img={url: "(.*?)",id', re.S)

    imglist = re.findall(reg, html)
    print len(imglist)
    for imgurl in imglist:
        print imgurl
        right = imgurl.rindex('/')
        name = imgurl[right+1:]
        if not imgurl.startswith('http'):
            imgurl=url+imgurl
        savepath = os.path.join("/home/david/Pictures/", name)
        urllib.urlretrieve(imgurl, savepath)

if __name__ == '__main__':
    get_bing_wallpaper()
展开阅读全文

没有更多推荐了,返回首页