• Python【Show Me The Code】小功能


    1、改改注册表,使游戏全屏:

      

    import win32api
    import win32con
    
    ChildKey = "SYSTEM\ControlSet001\Control\GraphicsDrivers\Configuration\CMN14760_33_07DB_14^CD1210F191996046BD4FAF7270B5C9BB\00\00"
    
    handle = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,ChildKey,0,win32con.KEY_ALL_ACCESS)
    
    getnum = win32api.RegQueryValueEx(handle,'Scaling')
    
    win32api.RegSetValueEx(handle,'Scaling',0,win32con.REG_DWORD,0x3)
    
    win32api.RegCloseKey(handle)

    2、往图片右上角添加数字,犹如qq消息提示一样:

    from PIL import Image
    from PIL import ImageFont
    from PIL import ImageDraw
    
    class Draw:
        def __init__(self,name):
            self.name = name
    
        def open(self):
            try:
                self.pic = Image.open(self.name)
            except:
                print("文件无法打开")
                exit(1)
    
        def draw_pic(self,num):
            self.tar_size = max(self.pic.size[0],self.pic.size[1])/5
            self.num = num
            self.tar_draw = str(self.num) if self.num < 100 else '99+'
            self.font = ImageFont.truetype("Arial.ttf",self.tar_size)
            self.px = self.pic.size[0] - self.font.getsize(self.tar_draw)[0]
    
            self.opened = ImageDraw.Draw(self.pic)
            self.opened.text((self.px,0),self.tar_draw,(255,0,0),self.font)
            self.pic.save("New.jpg")
            return True
    
    
    filename = raw_input("输入文件名:")
    obj = Draw(filename)
    obj.open()
    num = input('输入数量:')
    obj.draw_pic(num)

    3、爬虫:

    import urllib.request
    import os
    import re
    
    def url_open(url):
        req = urllib.request.Request(url)
        req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36')
        response = urllib.request.urlopen(url)
        html = response.read()
        return html
    
    def get_page(url):
    
        try:
                #print("正在尝试utf-8解码")
                html = url_open(url).decode("utf-8")
        except UnicodeDecodeError:
                #print("解码失败,尝试GBK解码")
                html = url_open(url).decode('GBK')
    
        rep = r'forum.php?mod=viewthread&amp;tid=([0-9]{5})'
        list1 = list(set(re.findall(rep,html)))
        #print(listone)
        print("函数get_page执行完毕。。。正在返回")
        return list1
    
    
    def page_open(list1):
        urlopen_page = 'http://bbs.fishc.com/forum.php?mod=viewthread&tid='
        os.mkdir('YuC')
        os.chdir('YuC')
        for each in list1:
            page_add = urlopen_page + each
            print("正在下载网址:" ,page_add)
            download_page(page_add)
    
    
    
    
    def download_page(page_add):
    
        get = url_open(page_add)
        filename = get_filename(get)
        
    
        #f1 = re.search(r'id="thread_subject">(.*)</span>',get)
        #filename = str(f1[0])
        print("创建文件当中.........")
        with open(filename,'wb') as f:
            f.write(get)
        print("文件创建完毕!")
    
    
    def get_filename(get):
        try:
                #print("正在尝试utf-8解码")
                get = get.decode("utf-8")
        except UnicodeDecodeError:
                #print("解码失败,尝试GBK解码")
                get = get.decode('GBK')
        f1 = re.findall(r'id="thread_subject">(.*)</span>',get)
        filename = str(f1[0]) +'.html'
        return filename
    
    
    
    
    if __name__ == '__main__':
        
        url = 'http://bbs.fishc.com/forum.php?mod=forumdisplay&fid=255&typeid=420&typeid=420&filter=typeid&page='
        for i in range(4):
            url = url + str(i)
            list1 = get_page(url)
            page_open(list1)

      

  • 相关阅读:
    jQuery让渡$操作符
    JSON
    Jquery实现Ajax(二)
    Jquery实现Ajax(一)
    Javascript实现Ajax
    文章目录
    读过的书
    JVM笔记(5)-垃圾回收&内存分配策略
    JVM笔记(4)-对象及其引用
    JVM笔记(3)-内存结构&方法执行(栈帧)
  • 原文地址:https://www.cnblogs.com/You0/p/4459814.html
Copyright © 2020-2023  润新知