BingBong架构采用MFC处理UI搭建、配置处理,Python实现爬虫模块的架构。调用时将对应的参数传入爬虫模块,随后爬虫开始下载。
Python代码相对简单,耗时的反而是找各种第三库的资料啊~
#!C:\Python27\python # -*- coding: utf-8 -*- import string,urllib2,re,time,sys import win32api, win32con, win32gui import Image #主函数 def BingBong(path,cho): response = urllib2.urlopen(‘http://cn.bing.com/‘) html = response.read() pattern = re.compile( r‘http://s.cn.bing.net‘ + ‘.*?\.jpg‘ ) match = pattern.search( html ) if match: #print u‘图片搜索成功!‘ #print match.group() conn = urllib2.urlopen( match.group() ) Date = time.strftime(‘%Y-%m-%d‘,time.localtime(time.time())) jpgPath = path + Date + ‘.jpg‘ f = open( jpgPath,‘wb‘ ) f.write(conn.read()) f.close() if cho: setWallPaper( jpgPath,sys.argv[3] ) #print u‘图片已保存!‘ else: print u‘图片搜索失败!‘ def setWallpaperFromBMP(imagepath): k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE) win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸适应桌面,0桌面居中 win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0") win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2) # convert jpg to bmp def setWallPaper(imagePath,SavePath): bmpImage = Image.open(imagePath) #newPath = imagePath.replace(‘.jpg‘, ‘.bmp‘) bmpImage.save(SavePath, "BMP") setWallpaperFromBMP(SavePath) #print u‘请输入保存图片的路径:‘ path = sys.argv[1] cho = int(sys.argv[2]) BingBong(path,cho)
时间: 2024-10-29 10:46:10