用Python抓取屏幕抓取信息

用Python获取命令行输出 将屏幕输出的log保存文件中
# -*- coding:utf-8 -*-
command = 'ping www.baidu.com '#可以直接在命令行中执行的命令
r = os.popen(command)
info = r.readlines()
for line in info:
line = line.strip('\r\n')
if isinstance(line, unicode):
cnstr = line.encode('utf-8')
cnstr = line.decode('cp936').encode('utf-8')
print cnstr
参考:http://blog.sina.com.cn/s/blog_6a6c136d0102v4zx.htmlhttp://www.cnblogs.com/liu-ke/p/5092274.html
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
python 信息同时输出到屏幕与文件
摘要:python信息同时输出到屏幕与文件python编程中,往往需要将结果用print等输出,如果希望输出既可以显示到IDE的屏幕上,也能存到文件中(如txt)中,该怎么办呢?方法1可通过日志logging模块输出信息到文件或屏幕。但可能要设置log的level或输出端,对于同时需要记录debugerror等信息的较为合适,官方教程推荐学习用更规范的logger来操作。例如,可参考来自官网的这段代码。importlogginglogging.basicConfig(filenam
python 信息同时输出到屏幕与文件
python编程中,往往需要将结果用print等输出,如果希望输出既可以显示到IDE的屏幕上,也能存到文件中(如txt)中,该怎么办呢?
可通过日志logging模块输出信息到文件或屏幕。但可能要设置log的level或输出端,对于同时需要记录debug error等信息的较为合适,官方教程推荐学习用更规范的logger来操作。 例如,可参考来自官网的这段代码。
import logginglogging.basicConfig(filename='log_examp.log',level=logging.DEBUG)logging.debug('This message should go to the log file')logging.info('So should this')logging.warning('And this, too')
利用print输出两次 比如这里我想输出程序的path和程序的文件名
import os# 第一句输出到consle:print(&filepath:&,__file__,&/nfilename:&,os.path.basename(__file__))# 第二句输出到txt:with open(&outputlog.txt&,&a+&) as f:
print(&filepath:&,__file__,
&/nfilename:&,os.path.basename(__file__))
#当然 也可以用f.write(&info&)的方式写入文件
利用输出重定向输出两次 同样输出程序path和文件名
import osimport systemp=sys.stdout # 记录当前输出指向,默认是conslewith open(&outputlog.txt&,&a+&) as f:
sys.stdout=f
# 输出指向txt文件
print(&filepath:&,__file__,
&/nfilename:&,os.path.basename(__file__))
print(&some other information&)
print(&some other&)
print(&information&)
sys.stdout=temp # 输出重定向回consle
print(f.readlinse()) # 将记录在文件中的结果输出到屏幕
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
邮箱低至5折
推荐购买再奖现金,最高25%
&200元/3月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
python 信息同时输出到屏幕与文件相关信息,包括
的信息,所有python 信息同时输出到屏幕与文件相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International运用python提取文本信息实战-rdoq time matching
RDOQ Time Matching 实战:
目标:提取rdoq的运行时间。
工程中rdoq运行时间用全局变量统计,编码结束后获得rdoq总的运行时间。
原始做法是将该变量全部连续的写到一个文件里,每次手动填表
python 批处理 结合的尝试 还未完全理解
但是将runtime写到屏幕上,-&转入log文件中,直接对log文件的信息做匹配提取,效果也是一样的佳。
要解决的问题:
1、匹配,好简单
2、遍历一个文件夹中的所有文件再做匹配
重点解决的是第二个
pathDir = os.listdir('C:/Users/wangm/Desktop/Bat/RA/log')
将路径下的所有的文件名当作字符串,写在了pathDir中,他是一个list
接下来的操作就可以按照粗暴的list循环的方式做
for filename in pathDir:
c = 'C:/Users/wangm/Desktop/Bat/RA/log/' + filename
file_object
= open(c, 'r')
print(file_object)
chunk = file_object.readline()
if chunk == '':
matchTime = 'RdoqTime'
time = matching(chunk, matchTime, [1])
write_time(time, filename, "0.00")
print(time)
另一种结局方式是,为了保证每次读取文件数据的顺序,可以先按照表格顺序建表~
c变量的构成还是粗爆的。
f_out = open('C:/Users/wangm/Desktop/Bat/RA/rdoq.txt', 'w+')
import string
def matching(chunk, matchsyntax, index):
if re.match(matchsyntax, chunk):
splitWord = re.split(r'\s+', chunk)
matchWord = []
for i in index:
matchWord.append(splitWord[i])
return matchWord
def write_time(time, filename, incline):
f_out.write("%-10s"%(time[0]))
f_out.write(" ")
f_out.write("%40s"%(filename))
f_out.write("
f_out.write(incline)
f_out.write("\n")
pathDir = os.listdir('C:/Users/wangm/Desktop/Bat/RA/log')
write_time(["Time"], "FileName", "0.00")
for filename in pathDir:
c = 'C:/Users/wangm/Desktop/Bat/RA/log/' + filename
file_object
= open(c, 'r')
print(file_object)
chunk = file_object.readline()
if chunk == '':
matchTime = 'RdoqTime'
time = matching(chunk, matchTime, [1])
write_time(time, filename, "0.00")
print(time)
file_object.close()
f_out.close()
在文本输出格式方面,对其什么的为了方便复制粘贴还是有讲究的
f_out.write(“%-10s”%(time[0]))
带格式的写文件
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!python 获取windows硬件信息和简单资产统计-vldc
需要安装wmi,pyinstaller模块,MonitorInfoView.exe用于获取显示器信息
pip install wmi pyinstaller
wmi用于调用win的com组件,pyinstaller用于打包程序成exe文件,由于没法获取到显示器信息,还需要调用第三方程序MonitorInfoView.exe
获取硬件配置的核心代码
import win32com
import wmi
import csv
c = wmi.WMI()
class WinCheck():
&&& def __init__(self):
&&&&&&& objWMIService = win32com.client.Dispatch(&WbemScripting.SWbemLocator&)
&&&&&&& self.objSWbemServices = objWMIService.ConnectServer(&.&, &root\cimv2&)
&&& def check_cpu(self):
&&&&&&& #检测CPU
&&&&&&& ret = []
&&&&&&& for cpu in c.Win32_Processor():
&&&&&&&&&&& ret.append({&name&: cpu.Name,
&&&&&&&&&&&&&&&&&&&&&&& &id&: cpu.ProcessorId,
&&&&&&&&&&&&&&&&&&&&&&& &core&: cpu.NumberOfCores,
&&&&&&&&&&&&&&&&&&&&&&& &numOfLogicalProcessors&: cpu.NumberOfLogicalProcessors})
&&&&&&& return ret
&&& def check_disk(self):
&&&&&&& #检测硬盘
&&&&&&& disk_cnt = 1
&&&&&&& ret = []
&&&&&&& for physical_disk in c.Win32_DiskDrive():
&&&&&&&&&&& ret.append({&id&: disk_cnt,
&&&&&&&&&&&&&&&&&&&&&&& &caption&: physical_disk.Caption,
&&&&&&&&&&&&&&&&&&&&&&& &size&: long(physical_disk.Size) / 1024 / 1024 / 1024})
&&&&&&&&&&& disk_cnt += 1
&&&&&&& return ret
&&& def check_monitor(self):
&&&&&&& #检测显示器
&&&&&&& r = os.system(&MonitorInfoView.exe /scomma monitor.csv&)
&&&&&&& if r == 0:
&&&&&&&&&&& if os.path.exists(&./monitor.csv&):
&&&&&&&&&&&&&&& reader = csv.reader(open(&monitor.csv&), delimiter=&,&)
&&&&&&&&&&& else:
&&&&&&&&&&&&&&& return False
&&&&&&&&&&& ret = []
&&&&&&&&&&& for line in reader:
&&&&&&&&&&&&&&& if line[0] or line[1] == &Yes&:
&&&&&&&&&&&&&&&&&&& # print line[8].decode(&utf-8&,&ignore&).encode(&utf-8&)
&&&&&&&&&&&&&&&&&&& ret.append({&name&: line[0], &SN&: line[2], &pix&: line[6], &size&: line[8].decode(&utf-8&, &ignore&).encode(&utf-8&)})
&&&&&&&&&&& return ret
&&&&&&& else:
&&&&&&&&&&& return r
&&& def check_mem(self):
&&&&&&& #检测内存
&&&&&&& colItems = self.objSWbemServices.ExecQuery(&Select * from Win32_PhysicalMemory&)
&&&&&&& ret = []
&&&&&&& for objItem in colItems:
&&&&&&&&&&& gb = int(1024 * 1024 * 1024)
&&&&&&&&&&& Memory_Size = int(objItem.Capacity) / gb
&&&&&&&&&&& if objItem.Manufacturer is None:
&&&&&&&&&&&&&&& objItem.Manufacturer = u'未知品牌'
&&&&&&&&&&& ret.append({&caption&: objItem.Manufacturer, &size&: Memory_Size})
&&&&&&& return ret
&&& def check_net(self):
&&&&&&& #检测网卡
&&&&&&& ret = []
&&&&&&& for interface in c.Win32_NetworkAdapterConfiguration(IPEnabled=1):
&&&&&&&&&&& ret.append({&mac&: interface.MACAddress, &ip&: interface.IPAddress[0], })
&&&&&&& return ret
&&& def check_display(self):
&&&&&&& #检测显卡
&&&&&&& colItems = self.objSWbemServices.ExecQuery(&Select * from Win32_VideoController&)
&&&&&&& ret = []
&&&&&&& for objItem in colItems:
&&&&&&&&&&& ret.append(objItem.Caption)
&&&&&&& return ret
想获取更多详细的信息wmi api说明https://msdn.microsoft.com/en-us/library/bgv=vs.85%29.aspx,基本上大部分硬件信息都有对应的api
3.把数据上传到服务器
server端用django
models.py:
class Property(models.Model):
&&& owner = models.CharField(max_length=50)
&&& date_time = models.DateTimeField(auto_now=True)
class CPU(models.Model):
&&& owner = models.ForeignKey(Property)
class Disk(models.Model):
&&& owner = models.ForeignKey(Property)
class Mem(models.Model):
&&& owner = models.ForeignKey(Property)
class Monitor(models.Model):
&&& owner = models.ForeignKey(Property)
class Netcard(models.Model):
&&&&&&& owner = models.ForeignKey(Property)
class Raw(models.Model):
&&& cpu = models.CharField(max_length=250, null=True, blank=True)
&&& disk = models.CharField(max_length=250, null=True, blank=True)
&&& mem = models.CharField(max_length=250, null=True, blank=True)
&&& monitor = models.CharField(max_length=250, null=True, blank=True)
&&& net = models.CharField(max_length=250, null=True, blank=True)
&&& videocard = models.CharField(max_length=250, null=True, blank=True)
&&& time = models.DateTimeField(auto_now=True)
from django.shortcuts import render_to_response
from django.http import HttpResponse
from models import *
import json
def index_view(request):
&&& if request.method == &POST&:
&&&&&&& net = request.POST.get(&net&, &&)
&&&&&&& cpu = request.POST.get(&cpu&, &&)
&&&&&&& disk = request.REQUEST.get(&disk&, &&)
&&&&&&& monitor = request.POST.get(&monitor&, &&)
&&&&&&& mem = request.POST.get(&mem&, &&)
&&&&&&& videocard = request.POST.get(&videocard&, &&)
&&&&&&& r = Raw(net=net, cpu=cpu, disk=disk, monitor=monitor, mem=mem, videocard=videocard)
&&&&&&& r.save()
&&&&&&& id = Raw.objects.latest('id').id
&&&&&&& return HttpResponse(id)
&&&&&&& id = 0
&&&&&&& try:
&&&&&&&&&&& id = Raw.objects.latest('id').id
&&&&&&& except:
&&&&&&&&&&& pass
&&&&&&& return HttpResponse(id)
&&& return eval(s)
def index_edit(request, property_id):
&&& id = property_id
&&& if id and request.method == &GET&:
&&&&&&& p = Raw.objects.get(id=id)
&&&&&&& return render_to_response('index.htm', {&net&: e(p.net), &cpu&: e(p.cpu),
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &disk&: e(p.disk),
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &monitor&: e(p.monitor),
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &mem&: e(p.mem),
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &videocard&: e(p.videocard)})
&&& &head&
&&& &&& &meta charset=&utf-8&&
&&& &&& &meta http-equiv=&X-UA-Compatible& content=&IE=edge,chrome=1&&
&&& &&& &meta name=&description& content=&Main app entry point&&
&&& &&& &meta name=&viewport& content=&width=device-width, initial-scale=1.0&&
&&& &&& &link rel=&stylesheet& href=&/static/bootstrap/css/bootstrap.min.css&&
&&& &&& &link rel=&stylesheet& href=&/static/bootstrap/css/bootstrap-responsive.min.css& &
&&& &&& &script type=&text/javascript& src=&/static/jquery-1.10.2.js&&&/script&
&&& &&& &div class=&container&&
&&& &&& &&& &h3&显示器&/h3&
&&& &&& &&& &div class=&table-responsive&&
&&& &&& &&& &&& &table class=&table table-striped table-bordered&&
&&& &&& &&& &&& &&& &thead&
&&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &th&显示器型号&/th&
&&& &&& &&& &&& &&& &&& &&& &th&显示器尺寸&/th&
&&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &/thead&
&&& &&& &&& &&& &&& &tbody&
&&& &&& &&& &&& &&& &&& {% for m in monitor %}
&&& &&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &&&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ m.name }}&/td&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ m.size }}&/td&
&&& &&& &&& &&& &&& &&& &&& &&&
&&& &&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &&& {% endfor %}
&&& &&& &&& &&& &&& &/tbody&
&&& &&& &&& &&& &/table&
&&& &&& &&& &&&
&&& &&& &&& &&& &table class=&table table-striped table-bordered&&
&&& &&& &&& &&& &&& &h3&显卡&/h3&
&&& &&& &&& &&& &&& &thead&
&&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &th&型号&/th&
&&& &&& &&& &&& &&& &&& &&&
&&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &/thead&
&&& &&& &&& &&& &&& &tbody&
&&& &&& &&& &&& &&& &&& {% for v in videocard %}
&&& &&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ v }}&/td&
&&& &&& &&& &&& &&& &&& &&& &&&
&&& &&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &&& {% endfor %}
&&& &&& &&& &&& &&& &/tbody&
&&& &&& &&& &&& &/table&
&&& &&& &&& &&& &table class=&table table-striped table-bordered&&
&&& &&& &&& &&& &&& &h3&cpu&/h3&
&&& &&& &&& &&& &&& &thead&
&&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &th&CPU型号&/th&
&&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &/thead&
&&& &&& &&& &&& &&& &tbody&
&&& &&& &&& &&& &&& &&& {% for cp in cpu %}
&&& &&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ cp.name }}&/td&
&&& &&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &&& {% endfor %}
&&& &&& &&& &&& &&& &/tbody&
&&& &&& &&& &&& &/table&
&&& &&& &&& &&& &table class=&table table-striped table-bordered&&
&&& &&& &&& &&& &&& &h3&硬盘&/h3&
&&& &&& &&& &&& &&& &thead&
&&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &th&厂商&/th&
&&& &&& &&& &&& &&& &&& &&& &th&容量(G)&/th&
&&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &/thead&
&&& &&& &&& &&& &&& &tbody&
&&& &&& &&& &&& &&& &&& {% for ds in disk %}
&&& &&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ ds.caption }}&/td&
&&& &&& &&& &&& &&& &&& &&& &&&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ ds.size }}&/td&
&&& &&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &&& {% endfor %}
&&& &&& &&& &&& &&& &/tbody&
&&& &&& &&& &&& &/table&
&&& &&& &&& &&& &table class=&table table-striped table-bordered&&
&&& &&& &&& &&& &&& &h3&网卡&/h3&
&&& &&& &&& &&& &&& &thead&
&&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &th&ip&/th&
&&& &&& &&& &&& &&& &&& &&& &th&mac&/th&
&&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &/thead&
&&& &&& &&& &&& &&& &tbody&
&&& &&& &&& &&& &&& &&& {% for n in net %}
&&& &&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ n.ip }}&/td&
&&& &&& &&& &&& &&& &&& &&& &&&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ n.mac }}&/td&
&&& &&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &&& {% endfor %}
&&& &&& &&& &&& &&& &/tbody&
&&& &&& &&& &&& &/table&
&&& &&& &&& &&& &table class=&table table-striped table-bordered&&
&&& &&& &&& &&& &&& &h3&内存&/h3&
&&& &&& &&& &&& &&& &thead&
&&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &th&厂商&/th&
&&& &&& &&& &&& &&& &&& &&& &th&容量(G)&/th&
&&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &/thead&
&&& &&& &&& &&& &&& &tbody&
&&& &&& &&& &&& &&& &&& {% for m in mem %}
&&& &&& &&& &&& &&& &&& &&& &tr&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ m.caption }}&/td&
&&& &&& &&& &&& &&& &&& &&& &&&
&&& &&& &&& &&& &&& &&& &&& &&& &td&{{ m.size }}&/td&
&&& &&& &&& &&& &&& &&& &&& &/tr&
&&& &&& &&& &&& &&& &&& {% endfor %}
&&& &&& &&& &&& &&& &/tbody&
&&& &&& &&& &&& &/table&
&&& &&& &&& &/div&
可以根据自己的需求在加上其他程序,如:获取不到的信息用户可以提交,机器使用人是谁,把数据提出来单独放在数据库里等等
3.打包程序
pyinstaller client.py
一些杀毒软件会包木马,可以用zprotoct,upx加壳,做免杀
很喜欢此文字
很喜欢此文字python屏幕的交互(读取输出信息)input,raw_input的区别
&&& input("your name?")
your name?sam
Traceback (most recent call last):
File "&pyshell#0&", line 1, in &module&
input("your name?")
File "&string&", line 1, in &module&
NameError: name 'sam' is not defined
可以看到,input函数必须使用字符串作为输出类型,即输出时加双引号引入。如下
&&& input("your name?")
your name?"sam"
如果,觉得双引号麻烦,则raw_input(“sting”)可以直接输入,不用双引号:
&&& raw_input("your name?")
your name?sam
不带双引号,也没有像input语句那样出错,这就是input与raw_input语句的区别。
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!

我要回帖

更多关于 有道屏幕文字抓取工具 的文章

 

随机推荐