每日一首古诗, 每日一个桌面壁纸

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

正好今天没课,于是闲不下来,做了两个小工具。

  • 每日古诗词
  • 每日壁纸

这两个小脚本都不难,所以很容易上手。


依赖

两个接口

这两个小工具的实现,离不开下面的这两个接口,分别是:

点击上面的链接就可以进入接口详情界面了,文档也很详细,就不在过多叙述了。

第三方库

由于需要访问网络并且操作系统壁纸,所以需要使用下面的库:

  • requests: 用于网络请求,获取结果
  • json: 用于处理返回的JSON数据
  • win32api, win32gui, win32con: 操作系统壁纸

每日诗词

最后做成的效果就是可以搜索诗词名称,作者名称的古诗词作品,并显示到终端中。方便学习和使用。

代码

# coding: utf-8import requestsimport jsondef getData(url, title=None, author=None, years=''):    headers = {        'apikey': '56eab527a0facb6670b552fd'    }    data = {        'title': title,        'author': author,        'year': years    }    rawdata = requests.post(url, data=data, headers=headers)    result = json.loads(rawdata.text.encode('utf-8'))    return resultdef showPoems(data):    query_result = data['msg']    query_size = data['count']    query_result = data['result']    for index in range(len(query_result)):        item = query_result[index]        print_length = len(item['infoText']) if len(item['infoText']) < 64 else 64        print '-'*(print_length), 'BEGIN %d'%(index+1), '-'*(print_length)        print item['title'], item['years'], item['author']        print item['infoText']        print '\n'    print '-'*28, 'END', '-'*28if __name__ == '__main__':    print "作者名称author, 诗词名称title 二选一, years为可选项,如唐朝,宋朝等".decode('utf-8').encode('gbk')    url = 'http://api.getlove.cn/api/poetry'    title = raw_input("Poem title: ").decode('gbk')    author = raw_input('Poem author: ').decode('gbk')    years = raw_input('Poem years: ').decode('gbk')    showPoems(getData(url, title=title, author=author, years=years))
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

效果图(gif)

  • 按古诗词标题查找

title

  • 按古诗词作者查找

author

每日桌面

代码

原理:
在使用win32con.SPI_SETDESKWALLPAPER设置Wallpaper时,其第二个参数为图片路径,图片必须是BMP格式。如下:

win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,  imagepath,  1+2)
   
   
  • 1

否则将报错如下:

pywintypes.error: (0, 'SystemParametersInfo', 'No error message is available')
   
   
  • 1
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,  tmppath,  0)
   
   
  • 1
  • 2

效果图(gif)

更换背景图片

总结

回顾一下,本次实验内容。复习了requests和win32api库的使用。

美中不足的是脚本仍然不够灵活,拓展性也不够强,

每日古诗词那个在终端中显示的界面效果不够良好,太粗糙。

每日桌面那个还可以更加人性化,比如做成定时更换壁纸等等。

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/gfdfhjj/article/details/83999656