红联Linux门户
Linux帮助

获取bing背景图片

发布时间:2017-02-26 10:08:30来源:linux网站作者:孙大胃
搜索引擎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()
 
本文永久更新地址:http://www.linuxdiyf.com/linux/28714.html