代码如下:
- from ctypes import *
- while True:
- user32 = windll.LoadLibrary('user32.dll')
- user32.LockWorkStation()
分享四:一款python代码的数字猜谜小游戏
代码如下:
- import random
- rang1 = int(input("请设置本局游戏的最小值:"))
- rang2 = int(input("请设置本局游戏的最大值:"))
- num = random.randint(rang1,rang2)
- guess = "guess"
- print("数字猜谜游戏!")
- i = 0
- while guess != num:
- i += 1
- guess = int(input("请输入你猜的数字:"))
-
- if guess == num:
- print("恭喜,你猜对了!")
- elif guess < num:
- print("你猜的数小了...")
- else:
- print("你猜的数大了...")
-
- print("你总共猜了%d" %i + "次",end = '')
- print(",快和你朋友较量一下...")
分享五:一段好玩的Python爬虫代码
这几年网络爬虫很火,用Python语言实现网络爬虫最合适不过了,接下来分享一段好玩的爬虫代码:
- # -*- coding: utf-8 -*-
- import urllib2
- import re
- class QSBK:
- def __init__(self):
- self.pageIndex = 1
- self.user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64)'
- self.headers = {'User-Agent': self.user_agent}
- self.stories = []
- # 存放程序是否继续运行的变量
- self.enable = False
- # 传入某一页的索引获得页面代码
- def getPage(self, pageIndex):
- try:
- url = 'http://www.qiushibaike.com/hot/page/' + str(pageIndex)
- request = urllib2.Request(url, headers = self.headers)
- response = urllib2.urlopen(request)
- pageCode = response.read().decode('utf-8')
- return pageCode
- except urllib2.URLError, e:
- if hasattr(e, "reason"):
- print u"连接糗事百科失败,错误原因", e.reason
- return None
-
- # 传入某一页代码,返回本页不带图片的段子列表
- def getPageItems(self, pageIndex):
- pageCode = self.getPage(pageIndex)
- if not pageCode:
- print "页面加载失败。。。"
- return None
- pattern = re.compile('<div class="author clearfix">.*?<h2>(.*?)</h2>.*?"content">(.*?)</div>.*?number">(.*?)</.*?number">(.*?)</.',re.S)
- items = re.findall(pattern, pageCode)
- pageStories = []
- for item in items:
- rereplaceBR = re.compile('<br/>')
- text = re.sub(replaceBR,"
- ",item[1])
-
- pageStories.append([item[0].strip(),text.strip(),item[2].strip(),item[3].strip()])
- return pageStories
-
- # 加载并提取页面内容,加入到列表中
- def loadPage(self):
- if self.enable == True:
- if len(self.stories) < 2:
- pageStories = self.getPageItems(self.pageIndex)
- if pageStories:
- self.stories.append(pageStories)
- self.pageIndex += 1
-
- # 调用该方法,回车打印一个段子
- def getOneStory(self, pageStories, page):
- for story in pageStories:
- input = raw_input()
- self.loadPage()
- if input == "Q":
- self.enable = False
- return
- print u"第%d页 发布人:%s 赞:%s 评论:%s
- %s" %(page,story[0],story[2],story[3],story[1])
-
- def start(self):
- print u"正在读取糗事百科,按回车查看新段子,Q退出"
- self.enable = True
- self.loadPage()
- nowPage = 0
- while self.enable:
- if len(self.stories) > 0:
- pageStories = self.stories[0]
- nowPage += 1
- del self.stories[0]
- self.getOneStory(pageStories, nowPage)
-
- spider = QSBK()
- spider.start()
分享六、木马程序常用的键盘记录功能实现
(编辑:ASP站长网)
|