最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)設(shè)置windows桌面壁紙代碼分享

 更新時(shí)間:2015年03月28日 11:57:05   投稿:junjie  
這篇文章主要介紹了Python實(shí)現(xiàn)設(shè)置windows桌面壁紙,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下

每天換一個(gè)壁紙,每天好心情。

# -*- coding: UTF-8 -*- 

from __future__ import unicode_literals
import Image
import datetime
import win32gui,win32con,win32api
import re
from HttpWrapper import SendRequest

StoreFolder = "c:\\dayImage"

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拉伸適應(yīng)桌面,0桌面居中
  win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
  win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)

def setWallPaper(imagePath):
  """
  Given a path to an image, convert it to bmp and set it as wallpaper
  """
  bmpImage = Image.open(imagePath)
  newPath = StoreFolder + '\\mywallpaper.bmp'
  bmpImage.save(newPath, "BMP")
  setWallpaperFromBMP(newPath)

def getPicture():
  url = "http://photography.nationalgeographic.com/photography/photo-of-the-day/"
  h = SendRequest(url)
  if h.GetSource():
    r = re.findall('<div class="download_link"><a href="(.*?)">Download',h.GetSource())
    if r:
      return SendRequest(r[0]).GetSource()
    else:
      print "解析圖片地址出錯(cuò),請(qǐng)檢查正則表達(dá)式是否正確"
      return None


def setWallpaperOfToday():
  img = getPicture()
  if img:
    path = StoreFolder + "\\%s.jpg" % datetime.date.today()
    f = open(path,"wb")
    f.write(img)
    f.close()
    setWallPaper(path)

setWallpaperOfToday()
print 'Wallpaper set ok!'


其中的httpwrapper是我寫的一個(gè)http訪問的封裝:

#!/usr/bin/env python 
# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name: 對(duì)http訪問的封裝
#
# Author: qianlifeng
#
# Created: 10-02-2012
#-------------------------------------------------------------------------------

import base64
import urllib
import urllib2
import time
import re
import sys

class SendRequest:
 """
 網(wǎng)頁(yè)請(qǐng)求增強(qiáng)類
 SendRequest('http://xxx.com',data=dict, type='POST', auth='base',user='xxx', password='xxx')
 """
 def __init__(self, url, data=None, method='GET', auth=None, user=None, password=None, cookie = None, **header):
  """
  url: 請(qǐng)求的url,不能為空
  date: 需要post的內(nèi)容,必須是字典
  method: Get 或者 Post,默認(rèn)為Get
  auth: 'base' 或者 'cookie'
  user: 用于base認(rèn)證的用戶名
  password: 用于base認(rèn)證的密碼
  cookie: 請(qǐng)求附帶的cookie,一般用于登錄后的認(rèn)證
  其他頭信息:
  e.g. referer='www.sina.com.cn'
  """

  self.url = url
  self.data = data
  self.method = method
  self.auth = auth
  self.user = user
  self.password = password
  self.cookie = cookie

  if 'referer' in header:
    self.referer = header[referer]
  else:
    self.referer = None

  if 'user-agent' in header:
    self.user_agent = header[user-agent]
  else:
## self.user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0'
    self.user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16'

  self.__SetupRequest()
  self.__SendRequest()

 def __SetupRequest(self):

  if self.url is None or self.url == '':
    raise 'url 不能為空!'

  #訪問方式設(shè)置
  if self.method.lower() == 'post':
    self.Req = urllib2.Request(self.url, urllib.urlencode(self.data))

  elif self.method.lower() == 'get':
    if self.data == None:
      self.Req = urllib2.Request(self.url)
    else:
      self.Req = urllib2.Request(self.url + '?' + urllib.urlencode(self.data))

  #設(shè)置認(rèn)證信息
  if self.auth == 'base':
    if self.user == None or self.password == None:
      raise 'The user or password was not given!'
    else:
      auth_info = base64.encodestring(self.user + ':' + self.password).replace('\n','')
      auth_info = 'Basic ' + auth_info
      self.Req.add_header("Authorization", auth_info)

  elif self.auth == 'cookie':
    if self.cookie == None:
      raise 'The cookie was not given!'
    else:
      self.Req.add_header("Cookie", self.cookie)


  if self.referer:
    self.Req.add_header('referer', self.referer)
  if self.user_agent:
    self.Req.add_header('user-agent', self.user_agent)


 def __SendRequest(self):

  try:
   self.Res = urllib2.urlopen(self.Req)
   self.source = self.Res.read()
   self.code = self.Res.getcode()
   self.head_dict = self.Res.info().dict
   self.Res.close()
  except:
   print "Error: HttpWrapper=>_SendRequest ", sys.exc_info()[1]


 def GetResponseCode(self):
  """
  得到服務(wù)器返回的狀態(tài)碼(200表示成功,404網(wǎng)頁(yè)不存在)
  """
  return self.code

 def GetSource(self):
  """
  得到網(wǎng)頁(yè)源代碼,需要解碼后在使用
  """
  if "source" in dir(self):
    return self.source
  return u''

 def GetHeaderInfo(self):
  """
  u'得到響應(yīng)頭信息'
  """
  return self.head_dict

 def GetCookie(self):
  """
  得到服務(wù)器返回的Cookie,一般用于登錄后續(xù)操作
  """
  if 'set-cookie' in self.head_dict:
   return self.head_dict['set-cookie']
  else:
   return None

 def GetContentType(self):
  """
  得到返回類型
  """
  if 'content-type' in self.head_dict:
   return self.head_dict['content-type']
  else:
   return None

 def GetCharset(self):
  """
  嘗試得到網(wǎng)頁(yè)的編碼
  如果得不到返回None
  """
  contentType = self.GetContentType()
  if contentType is not None:
    index = contentType.find("charset")
    if index > 0:
      return contentType[index+8:]
  return None

 def GetExpiresTime(self):
  """
  得到網(wǎng)頁(yè)過期時(shí)間
  """
  if 'expires' in self.head_dict:
   return self.head_dict['expires']
  else:
   return None

 def GetServerName(self):
  """
  得到服務(wù)器名字
  """
  if 'server' in self.head_dict:
   return self.head_dict['server']
  else:
   return None

__all__ = [SendRequest,]

if __name__ == '__main__':
  b = SendRequest("http://www.baidu.com")
  print b.GetSource()

相關(guān)文章

  • Django如何實(shí)現(xiàn)內(nèi)容緩存示例詳解

    Django如何實(shí)現(xiàn)內(nèi)容緩存示例詳解

    緩存對(duì)于大家來說應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于Django如何實(shí)現(xiàn)內(nèi)容緩存的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-09-09
  • 使用python設(shè)置Excel工作表網(wǎng)格線的隱藏與顯示

    使用python設(shè)置Excel工作表網(wǎng)格線的隱藏與顯示

    Excel表格界面的直觀性很大程度上得益于表格中的網(wǎng)格線設(shè)計(jì),這些線條幫助用戶精確對(duì)齊數(shù)據(jù),清晰劃分單元格,本文將介紹如何使用Python設(shè)置隱藏或顯示Excel工作表的網(wǎng)格線,實(shí)現(xiàn)自動(dòng)話及批量處理,感興趣的朋友可以參考下
    2024-06-06
  • python中三種輸出格式總結(jié)(%,format,f-string)

    python中三種輸出格式總結(jié)(%,format,f-string)

    在Python語(yǔ)言編程中,我們會(huì)與字符串打交道,那務(wù)必會(huì)輸出字符串來查看字符串的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于python中三種輸出格式的相關(guān)資料,三種格式分別是%,format,f-string,需要的朋友可以參考下
    2022-03-03
  • Python抓取手機(jī)號(hào)歸屬地信息示例代碼

    Python抓取手機(jī)號(hào)歸屬地信息示例代碼

    之前看到一篇文章有提供手機(jī)號(hào)歸屬地?cái)?shù)據(jù)庫(kù)的下載,由于手機(jī)號(hào)號(hào)段一直在增加,所以提供的數(shù)據(jù)基本上隨時(shí)會(huì)過期,更理想的方法是從網(wǎng)上定期抓取其他站點(diǎn)維護(hù)的經(jīng)緯度信息。下面這篇文章就給大家介紹了如何利用Python抓取手機(jī)歸屬地信息,有需要的朋友們可以參考借鑒。
    2016-11-11
  • Python實(shí)現(xiàn)求一個(gè)集合所有子集的示例

    Python實(shí)現(xiàn)求一個(gè)集合所有子集的示例

    今天小編就為大家分享一篇Python 實(shí)現(xiàn)求一個(gè)集合所有子集的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python設(shè)計(jì)模式之橋接模式原理與用法實(shí)例分析

    Python設(shè)計(jì)模式之橋接模式原理與用法實(shí)例分析

    這篇文章主要介紹了Python設(shè)計(jì)模式之橋接模式原理與用法,結(jié)合具體實(shí)例形式分析了Python橋接模式的相關(guān)概念、原理、定義及使用方法,需要的朋友可以參考下
    2019-01-01
  • Python+OpenCV 圖像邊緣檢測(cè)四種實(shí)現(xiàn)方法

    Python+OpenCV 圖像邊緣檢測(cè)四種實(shí)現(xiàn)方法

    本文主要介紹了通過OpenCV中Sobel算子、Schaar算子、Laplacian算子以及Canny分別實(shí)現(xiàn)圖像邊緣檢測(cè)并總結(jié)了四者的優(yōu)缺點(diǎn),感興趣的同學(xué)可以參考一下
    2021-11-11
  • No module named ‘win32gui‘ 的解決方法(踩坑之旅)

    No module named ‘win32gui‘ 的解決方法(踩坑之旅)

    這篇文章主要介紹了No module named ‘win32gui‘ 的解決方法(踩坑之旅),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 使用 Python 合并多個(gè)格式一致的 Excel 文件(推薦)

    使用 Python 合并多個(gè)格式一致的 Excel 文件(推薦)

    這篇文章主要介紹了使用 Python 合并多個(gè)格式一致的 Excel 文件,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python進(jìn)階之Excel基本操作介紹

    Python進(jìn)階之Excel基本操作介紹

    在現(xiàn)實(shí)中,很多工作都需要與數(shù)據(jù)打交道,Excel 作為常用的數(shù)據(jù)處理工具,一直備受人們的青睞,本文主要為大家介紹了一些Python中Excel的基本操作,希望對(duì)大家有所幫助
    2025-01-01

最新評(píng)論

金华市| 洛阳市| 黎川县| 德庆县| 嵊州市| 那曲县| 永春县| 梁山县| 江源县| 三亚市| 安多县| 错那县| 绥中县| 河曲县| 大同市| 东方市| 和硕县| 措勤县| 阜康市| 视频| 英山县| 和政县| 高雄县| 尚义县| 大冶市| 鄂尔多斯市| 龙江县| 资阳市| 南漳县| 隆德县| 鹤峰县| 台南市| 女性| 朔州市| 肥城市| 龙里县| 仙居县| 措勤县| 东明县| 靖安县| 白河县|