寻觅生命中的那一片浅草......

文章带标签 Python

Python处理keepass导出的xml文件

用keepass管理密码
现在有员工需要将用户名和密码导出来
格式如下
username password

我觉得最难的地方是根据Value取出来的list中,有空值,如果是空值,取其值或者类型时,都会提示
“AttributeError: ‘NoneType’ object has no attribute ‘nodeType’”
可以加if i.firstChild is None:continue进行判断,如果是空值则跳出本次循环
if i.firstChild is None:continue这个是非常pythonic的写法哦

程序

[python]
#!/bin/env python
import sys
from xml.dom import minidom
if len(sys.argv[0:]) != 2:
print “./keepass.py your xml file’s path”
sys.exit()

list=[]
xmldoc = minidom.parse(sys.argv[1])
reflist = xmldoc.getElementsByTagName(‘Value’)
for i in reflist[::-1]:
if i.firstChild is None:continue
list.append(i.firstChild.nodeValue)

for j in range(0,len(list),3):
print list[j]+” “+list[j+2]

[/python]

输出结果
lisi BiPqIcbHVfUDxaONKjNV
zhangsan h5LGhEYc3OASZf1VcGbD

keepass.xml

[xml]



KeePass

2010-10-18T06:08:30Z

2010-10-18T06:08:30Z

2010-10-18T06:08:30Z
365
2010-11-12T05:41:29Z
-1
-1

False
False
True
False
False

True
c8kwCXpGOE+MBy1jzCOT/g==
2010-10-18T06:08:30Z
AAAAAAAAAAAAAAAAAAAAAA==
2010-10-18T06:08:30Z
NHp/A6+c1kqAdPJrncQ5mQ==
Vgbin7aoeEuGRJXKiJk47g==




NHp/A6+c1kqAdPJrncQ5mQ==
keepass

48

2010-12-20T13:00:58Z
2010-12-20T13:00:52Z
2010-12-20T13:01:06Z
2010-12-18T10:04:30Z
False
3
2010-12-20T13:00:52Z

True

null
null
NMjEkhZudUiMAnPQOYQa+Q==

NMjEkhZudUiMAnPQOYQa+Q==
0





2010-12-20T13:01:18Z
2010-12-20T13:01:07Z
2010-12-20T13:01:18Z
2010-12-18T10:04:30Z
False
1
2010-12-20T13:01:07Z


Notes



Password
h5LGhEYc3OASZf1VcGbD


Title
张三


URL



UserName
zhangsan


True
0




b3KWP7kC+UmZnnSA4d0byg==
0





2010-12-20T13:01:31Z
2010-12-20T13:01:22Z
2010-12-20T13:01:31Z
2010-12-18T10:04:30Z
False
1
2010-12-20T13:01:22Z


Notes



Password
BiPqIcbHVfUDxaONKjNV


Title
李四


URL



UserName
lisi


True
0






9LVoDcHI9UquYgzy1tRXzg==
2010-11-12T05:49:57Z


gimxTDSBBkWdSIFORZUx0w==
2010-11-12T05:50:02Z


7/7Rd5KBT0CyY9O9A2RDpA==
2010-11-12T05:50:05Z


tPT9aQPrNkOM2mmj6kP3CA==
2010-11-16T02:58:07Z


gN/uXsDF206oc1g783tsLQ==
2010-11-16T02:58:11Z


jMYxMK3iakOWgPJGVz9lSg==
2010-11-22T08:55:46Z


j8UaFYCZbE2drG+gNysIwg==
2010-11-26T08:30:23Z


13QkVRyovk+9TNqxneYO5w==
2010-11-26T08:33:04Z


1+DFhfzAYUmL74gsRFzZNw==
2010-12-20T12:41:13Z




[/xml]

Python+bash下载“不许联想”的mp3

主要是学习Python处理xml的方法
先上一个纯Python的,有问题,部分歌曲下载不到,返回404错误,待解决

2010-12-15
终于找到404的原因了,今天早上回来,又执行了一下脚本,发现能下载的文件名都是没有空格的,而404的则都有空格,对文件名进行了编码,解决了,另外,由于有首歌的歌名有个中文的单引号,故需要将它编码为gbk,才发现Python2.4.3不支持gbk,所以,最后的程序,开头用的是Python2.7.1 ,修改的程序见页面底部

[python]
#!/bin/env python
import os
import urllib
from xml.dom import minidom
import time
download_time=time.strftime(‘%Y%m%d’,time.localtime())
download_path=os.getcwd()+”/”+”wxf”+download_time

if not os.path.isdir(download_path):
os.mkdir(download_path)

usock = urllib.urlopen(‘http://www.wangxiaofeng.net/mp3player.xml’)
xmldoc = minidom.parse(usock)
usock.close()
songlist = xmldoc.getElementsByTagName(‘song’)
for i in range(0,len(songlist),1):
url=songlist[i].attributes[“path”].value.encode(‘utf-8’)
name=songlist[i].attributes[“title”].value
urllib.urlretrieve(url,download_path+”/”+name+”.mp3″)
[/python]

改良下,结合bash处理

[python]
#!/bin/env python
import os
import urllib
from xml.dom import minidom
import time
download_time=time.strftime(‘%Y%m%d’,time.localtime())
download_path=os.getcwd()+”/”+”wxf”+download_time

if not os.path.isdir(download_path):
os.mkdir(download_path)

usock = urllib.urlopen(‘http://www.wangxiaofeng.net/mp3player.xml’)
xmldoc = minidom.parse(usock)
usock.close()
songlist = xmldoc.getElementsByTagName(‘song’)
f=open(‘mp3list.txt’,’w’)
for i in range(0,len(songlist),1):
url=songlist[i].attributes[“path”].value.encode(‘utf-8’)
f.write(url+”\n”)

f.close()

[/python]

[bash]
#!/bin/bash

mkdir /home/python_code/wxf20101214
cd !$
cat /home/python_code/mp3list.txt |while read line
do
wget “${line}”
done
[/bash]

url编码后的程序
[python]
#!/usr/local/python271/bin/python
import os
import urllib
from xml.dom import minidom
import time
download_time=time.strftime(‘%Y%m%d’,time.localtime())
download_path=os.getcwd()+”/”+”wxf”+download_time

if not os.path.isdir(download_path):
os.mkdir(download_path)

usock = urllib.urlopen(‘http://www.wangxiaofeng.net/mp3player.xml’)
xmldoc = minidom.parse(usock)
usock.close()
songlist = xmldoc.getElementsByTagName(‘song’)
for i in range(0,len(songlist),1):
url=urllib.quote(songlist[i].attributes[“path”].value.encode(‘gbk’),safe=”:/”)
name=songlist[i].attributes[“title”].value
urllib.urlretrieve(url,download_path+”/”+name+”.mp3″)

[/python]

批量下载某网站pdf文件down_pdf.py

最近在学Python,练手作品

[python]
#!/bin/env python
import urllib
import re
import os
from sgmllib import SGMLParser

found=[]

class URLLister(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.urls = []

def start_a(self, attrs):
href = [v for k, v in attrs if k==’href’]
if href:
self.urls.extend(href)

if not os.path.isdir(os.getcwd()+’/ppts’):
os.mkdir(os.getcwd()+’/ppts’)

usock = urllib.urlopen(“http://velocity.oreilly.com.cn/index.php?func=slidesvideos”)
parser = URLLister()
parser.feed(usock.read())
usock.close()
parser.close()
for url in parser.urls:
found.append(re.findall(‘ppts/\w*.pdf’,url))

for pdfurl in found:
for i in range(0,len(pdfurl),1):
urllib.urlretrieve(“http://velocity.oreilly.com.cn/”+pdfurl[i],os.getcwd()+”/”+pdfurl[i])

[/python]

python2.7.1+pysvn on CentOS

CentOS上的Python真是老掉牙了,2.4.3
我需要用python去update svn

主要参考了pysvn-1.7.2里的INSTALL.html

Subversion常规安装,安装目录
/opt/svn_1.6.12

Python安装目录
/usr/local/python271

mkdir /data/download
cd !$
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tar.bz2
wget http://pysvn.barrys-emacs.org/source_kits/pysvn-1.7.2.tar.gz

安装Python 2.7.1
tar xjf Python-2.7.1.tar.bz2
cd Python-2.7.1
./configure –prefix=/usr/local/python271
make && make install

安装pysvn
tar xzf pysvn-1.7.2.tar.gz
cd pysvn-1.7.2/Source
/usr/local/python271/bin/python setup.py configure –svn-root-dir=/opt/svn_1.6.12
make
mkdir -p /usr/local/python271/lib/python2.7/pysvn
/bin/cp -a pysvn/* /usr/local/python271/lib/python2.7/pysvn/

测试
vi svn_up.py

[python]

#!/usr/local/python271/bin/python
import pysvn
client=pysvn.Client()
client.update(‘/data/website/’)
[/python]

2018年九月
« 2月    
 12
3456789
10111213141516
17181920212223
24252627282930