看着网上抓取网页数据的文章直瞪眼
后来想到用字符串分割来提取相应部分的内容
程序简单,但数行数和下标费了很长时间
我知道这肯定不是最好的办法- -!!
但我实现了,哈哈
# -*- coding: cp936 -*-
from urllib import *
import re
def stockSearch():
baseurl="http://www.google.cn/finance?q=SHA%3A"
stockNo=raw_input("请输入股票代码:")
fullUrl=baseurl+stockNo
#print fullUrl
stockPage=urlopen(fullUrl)
pageInfo=stockPage.readlines()
stockNameLine=pageInfo[4]
stockNameList=re.split('[ >]',stockNameLine)
print "您所查询的股票是:"+stockNameList[1]
stockInfoLine=pageInfo[184]
stockInfoList=re.split('values',stockInfoLine)
stockInfoDetail=re.split('"',stockInfoList[1])
print "现价:"+stockInfoDetail[3]
print "涨幅: "+ ......
如果python调用外部程序,需要直接抓去命令行的输出,有什么好的办法呢?
这里我们需要用到 os.popen 这个管道,然后用 read、readline或者readlines来读取命令行输出
#需要执行的命令
strCommand = 'xxxxxxxxxxxxxxxxx'
#用popen来执行命令行
oStdout = os.popen(strCommand)
#假设输出的内容只有一行
strStdout = oStdout.readline()
#如果输入的内容读取有回车符号,可以用strip来消除,可得到完美的输出
strOut = strStdout.strip() ......
Python操作Excel方法:
(1)在sourceforge.net上有一个扩展模块叫pyXLWriter,可以方便的写Excel文件。
(2)下载win32com包装上,这个包可以调用windows的com及API函数等这类的功能。Python利用win32com操作Excel。
例子:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from win32com.client import Dispatch
import win32com.client
class easyExcel:
"""A utility to make it easier to get at Excel. Remembering
to save the data is your problem, as is error handling.
Operates on one workbook at a time."""
def __init__(self, filename=None):
self.xlApp = win32com.client.Dispatch('Excel.Application')
if filename: ......
Mako是什么?Moko是Python写的一个模板库,Python官网python.org用的就是它哦。其他废话也就不累赘了,直接来点代码,方便阅读与了解把。
(Mako官网地址:http://www.makotemplates.org/ ,可以下载安装包,推荐使用easy_install安装)
from mako.template import Template
mytemplate = Template("hello world!")
print mytemplate.render()
mytemplate = Template("hello, ${name}!")
print mytemplate.render(name="jack")
代码可以参考官方doc部分
mytemplate = Template(filename='/docs/mytmpl.txt')
print mytemplate.render()
还可以从设置模板为文件,设置filename属性
mytemplate = Template(filename='/docs/mytmpl.txt', module_directory='/tmp/mako_modules')
print mytemplate.render()
文件还可以缓存到某个目录下,下面的/docs/mytmpl.txt会产生一个py:/tmp/mako_modules/docs/mytmpl.txt.py
from mako.lookup import TemplateLookup
mylookup = TemplateLookup(directories=['/docs'])
mytemplate = Template("""<%include file="header.txt"/> hello world!""", lookup=mylookup)
查找模板,方便统一模板路径使用。
mylook ......
最近在学习py,实践是:校验指定文件是否打上数字签名~python的标准库中没有提供这类函数,同时在网上搜了一下也没有找到第三方的包有提供,最后找到了可以使用Crypt32.dll的导出函数CryptQueryObject()。
首先我希望pywin32的包中最好已封装这样的api,结果发现没有-_-!。于是不得不自己动手~py中调用dll的导出函数方法比较简单:
from ctypes import *
CryptQueryObject=windll.LoadLibrary("Crypt32.dll").CryptQueryObject
可是接下来问题来了。。CryptQueryObject中的参数怎么处理。。
BOOL WINAPI CryptQueryObject(
__in DWORD dwObjectType,
__in const void* pvObject,
__in DWORD dwExpectedContentTypeFlags,
__in DWORD dwExpectedFormatTypeFlags,
__in DWORD dwFlags,
__out DWORD* pdwMsgAndCertEncodingType,
__out DWORD* pdwContentType,
__out DWORD* pdwFormatType,
__out HCERTSTORE* phCertStore,
__out HCRYPTMSG* phMsg,
__out const void** ppvContext
);
参数类型中有数字, ......
接下来三行设置三种功能,treeview.set_search_column(0),当在界面中按“Control+F”时可以搜索第0列中的内容,tvcolumn.set_sort_column_id(0),当点击此列标题的时候所有的行按大小顺序重新排列。参数0是
列在Model里的序号。接下来treeview.set_reorderable方法使得可以用鼠标拖动行进行重新排列,比如,把第二行拖到第一行前,或者拖到第一行内,让第二行变成第一行的子节点(只有TreeStore可以做到这一点)。
下面就来改改这个程序,让它变成一个文件浏览器。
#
!/usr/bin/python
import
gtk
import
os
from
mainw
import
MainWindow
class
BasicTreeview(MainWindow):
def
get_widget(self):
dirname
=
u
"
E:\\lame-3.97
" #Fix Me:You should replace it with any valid dirname in your system.
dir_iter_dic
=
{dirname:None}
treestore
=
gtk.TreeStore(str,s ......
接下来三行设置三种功能,treeview.set_search_column(0),当在界面中按“Control+F”时可以搜索第0列中的内容,tvcolumn.set_sort_column_id(0),当点击此列标题的时候所有的行按大小顺序重新排列。参数0是
列在Model里的序号。接下来treeview.set_reorderable方法使得可以用鼠标拖动行进行重新排列,比如,把第二行拖到第一行前,或者拖到第一行内,让第二行变成第一行的子节点(只有TreeStore可以做到这一点)。
下面就来改改这个程序,让它变成一个文件浏览器。
#
!/usr/bin/python
import
gtk
import
os
from
mainw
import
MainWindow
class
BasicTreeview(MainWindow):
def
get_widget(self):
dirname
=
u
"
E:\\lame-3.97
" #Fix Me:You should replace it with any valid dirname in your system.
dir_iter_dic
=
{dirname:None}
treestore
=
gtk.TreeStore(str,s ......