第一步:连接数据库
使用Python可以实现对数据库的操作。
本例中,我们用两种方法实现了数据库的连接,并将数据取了出来。这两种方式都比较简单清楚:
1.
#!/usr/bin/python # -*- coding: utf-8 -*- import MySQLdb conn = MySQLdb.connect(host="192.16.0.12",user="walk",passwd="sn,Z1gddrB#911",db="netgame_trade",charset='utf8') cur = conn.cursor() #执行数据库的操作cur.execute cur.execute('select ID,GOODS_ID,GAME_ID,SERVER_ID,CREATED_BY,CREATED_BY_INFO,ATTRS_VALUE from SELLING_GOODS where ID=1963259') rows = cur.fetchall() print rows f=open('goods.txt', 'w') f.write(str(rows)) f.close() conn.close()导入对应包,
建立连接,设定连接,用户名密码,数据库名,编码方式,
执行sql语句,输出rows,
rows中包含的就是sql语句执行后取出来的内容,
写入txt,这里我们可以看到,Python将数据写入txt是很方便的,不需要导入什么包,只需要open、write、close就可以了。
2.使用DBUtil
from myyutil.DBUtil import DBUtil
tradeDBUtil = DBUtil('netgame_trade')
sql='select ID,GOODS_ID,NAME,CREATED_BY,CREATED_BY_INFO,GAME_ID,SERVER_ID,ATTRS_VALUE from SELLING_GOODS where ID=1963259' rows = tradeDBUtil.queryList(sql)这样也可以得到rows
第一句代码中,只需要写连接哪个库就可以了,其他的东西,工具模块中都已经写好啦。
第二步:将数据写入txt
Python中将数据写入txt很简单,open、write、close即可写入。
f=open('goods.txt', 'w') f.write(str(rows)) f.close()但写入的类型,这里指定为字符串。
逻辑简单,而最终版我们的写入方法如下:
def writeTxt(idNumber): #写入txt文件 f = open(fileName, 'a') rows=getInfo(idNumber) for row in rows: f.write("============================="+str(row[0])+"============================================"+"\n") f.write("ID: "+str(row[0])+"\n") f.write("商品ID: ".decode('utf-8').encode('gbk')+str(row[1])+"\n") f.write("商品名称: ".decode('utf-8').encode('gbk')+str(row[2]).decode('utf-8').encode('gbk')+"\n") f.write("用户ID: ".decode('utf-8').encode('gbk')+str(row[3])+"\n") f.write("用户昵称: ".decode('utf-8').encode('gbk')+str(row[4]).decode('utf-8').encode('gbk')+"\n") f.write("游戏名称: ".decode('utf-8').encode('gbk')+getGameName(idNumber).decode('utf-8').encode('gbk')+'\n') f.write("服务器名称: ".decode('utf-8').encode('gbk')+getServerName(idNumber).decode('utf-8').encode('gbk')+'\n\n') writeXml(idNumber, f) f.close()
其中有个遍历,是因为Python从数据库中取出的数据都是放在rows中的,rows打印出来如下:
((1963730L, 685L, '50\xe5\x85\x83=3650000\xe9\x87\x91\xe5\xb8\x81', 618027L, '\xe6\xbc\x8f\xe8\xa6\x81\xe6\x8d\xa1\xe6\xbc\x8f', 138L, 2718L, '
我们可以看到,是个元组类型的数据。
而且套了两层,所以我们需要遍历一次,然后再选一次。
所以,不能直接打印rows[0]!!!
分析:解析xml
上述的rows,我们可以看到,最后一个是xml。
我们要想从这样的一长串东西中取出想要的,就需要进行解析。回忆一下,之前我们解析过HTML,用到了beautifulsoup。
这里我们还需要用到这个工具模块。
我们单独写一个函数用来解析并写入:
def writeXml(idNumber,f): #解析xml并写入 rows=getInfo(idNumber) for row in rows: for i in range(row.__len__()): xml=row[7] soup = BeautifulSoup(xml, "html.parser", from_encoding="gbk") id = soup.find_all(name="id", attrs={}, text=re.compile("\S")) id=delIdTag(id) name = soup.find_all(name="name", attrs={}, text=re.compile("\S")) name=delNameTag(name) value = soup.find_all(name="value", attrs={}, text=re.compile("\S")) value=delValueTag(value) for i in range(id.__len__()): f.write('----------------------------------------------------'+'\n') f.write("ID: "+id[i]+'\n') f.write(name[i].decode('utf-8').encode('gbk')+': ') f.write(value[i].decode('utf-8').encode('gbk')+'\n\n')归纳一下:
先得到rows元组类型数据;
再用
for row in rows:去掉外面一层括号;
然后
for i in range(row.__len__()): xml=row[7]
row[7]取到xml,赋给xml;
再使用beautifulsoup将xml赋给一个soup对象;
然后就可以用我们比较熟悉的方式进行解析了;
这里比较方便,不同类型的值对应不同的tag;
由于这三个tag,三个一组,有很多组,所以我们来遍历写入,遍历的次数就是数组的长度。
贴出使用次数最多的,最初取数据函数getInfo()
def getInfo(idNumber): sql='select ID,GOODS_ID,NAME,CREATED_BY,CREATED_BY_INFO,GAME_ID,SERVER_ID,ATTRS_VALUE from SELLING_GOODS where ID=%s'%idNumber rows = tradeDBUtil.queryList(sql) return rows
getGameName和getServerName方法如下:
def getGameName(idNumber): #获取游戏名称 rows=getInfo(idNumber) for row in rows: for i in range(row.__len__()): gameid=row[5] sql='select NAME from GAME where ID=%s'%gameid rows2 = tradeDBUtil.queryList(sql) for row in rows2: for i in range(row.__len__()): gamename=row[0] return gamename
def getServerName(idNumber): #获取服务器名称 rows=getInfo(idNumber) for row in rows: for i in range(row.__len__()): serverid=row[6] for row in rows: for i in range(row.__len__()): gameid=row[5] sql='select NAME from SERVER where GAME_ID=%s and ID=%s'%(gameid, serverid) rows3 = tradeDBUtil.queryList(sql) for row in rows3: for i in range(row.__len__()): servername=row[0] return servername
逻辑都是一样的,简单归纳一下:
取到rows;
去掉括号;
for i in range(row.__len__()):
row[6] 得到数据;
一样的逻辑调用,取到我们要的游戏名称与服务器名称。返回值!