Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18145
  • 博文数量: 3
  • 博客积分: 146
  • 博客等级: 民兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-10 18:27
文章分类

全部博文(3)

文章存档

2012年(3)

最近访客

分类: Python/Ruby

2012-01-10 19:57:29

  1. #-------------------------------------------------------------------------------
  2. # Name: webAns
  3. # Purpose: 3366游戏抓取
  4. #
  5. # Author: winxos
  6. #
  7. # Created: 17/11/2011
  8. # Copyright: (c) winxos 2011
  9. # Licence: free
  10. #-------------------------------------------------------------------------------
  11. #!/usr/bin/env python
  12. # -*- coding: utf-8 -*-
  13. import urllib2
  14. import re
  15. from sqlite3 import *
  16. import os
  17. from time import clock
  18. import sys


  19. #游戏数据结构
  20. class gameinfo:
  21.     def __init__(self):
  22.         self.name=''#名称
  23.         self.id=0#页面ID
  24.         self.type=''#类型
  25.         self.tag=''#标签
  26.         self.width=0#宽度
  27.         self.heigh=0#高度
  28.         self.score=0.0#评分
  29.         self.detail=''#详情
  30.     def ToString(self):
  31.         return str(self.id)+self.name+self.type+str(self.width)+str(self.heigh)+\
  32.                 self.SRC()+" ".join(self.tag)+str(self.score)+self.detail
  33.     def SRC(self):#链接
  34.         a1=int(self.id)%100
  35.         return ""+str(a1)+"/"+str(self.id)+".swf"
  36. #进度保存类
  37. class ProgressSaver:
  38.     def Save(self,msg):
  39.         ofile=open('config.txt','w')
  40.         ofile.write(msg)
  41.         ofile.close()
  42.     def Load(self):
  43.         ret=''
  44.         if os.path.exists('config.txt'):#要判断文件是否存在
  45.             ifile=open('config.txt')
  46.             ret=ifile.read()
  47.         return ret

  48. def GetGameInfo(gameid):
  49.     uri=""+str(gameid)+".shtml"
  50.     print "visit:%s"%uri
  51.     try:
  52.         page=urllib2.urlopen(uri,timeout=3) #超时
  53.         data=page.read()
  54.         page.close()
  55.     except Exception,e: #捕捉访问异常,一般为timeout,信息在e中
  56.         return None


  57.     g=gameinfo()
  58.     g.id=int(gameid)
  59.     p1=re.compile('FlashPage\((.*)\)') #得到游戏主要信息
  60.     m1=p1.search(data)
  61.     if m1!=None:
  62.         s=m1.group(1)
  63.         s=s.replace('"','')#去掉引号
  64.         s=s.replace(' ','')#去掉空格p
  65.         ans=s.split(',')
  66.         g.date=ans[15]
  67.         g.width=int(ans[4])
  68.         g.heigh=int(ans[5])
  69.         g.name=ans[1]
  70.         g.type=ans[7]

  71.     p2=re.compile('t_tag">(\W*))#得到标签
  72.     m2=p2.findall(data)
  73.     g.tag=' '.join(m2)

  74.     p3=re.compile('score_num">(\d.\d)<')#得到分数
  75.     m3=p3.search(data)
  76.     if None:
  77.         g.score=float(m3.group(1))
  78.     p4=re.compile('\xd3\xce\xcf\xb7\W*\s*

    (\W*)

    '
    )#得到游戏简介,注意空白字符的匹配
  79.     m4=p4.search(data)
  80.     if m4!=None:
  81.         tmp=m4.group(1)
  82.         g.detail=tmp.replace('"','')
  83.     else:
  84.         print uri
  85.     return g



  86. #建立详细游戏资料库,包含id,名字,标签,源地址,宽高,说明等
  87. def CreateDetailLib():
  88.     ifile=open("e:/3366list.txt").read()
  89.     pt=re.compile('(\d+),')
  90.     m=pt.findall(ifile)
  91.     mu=list(set(m))#去除重复元素
  92.     if os.path.isfile('gamedetail.db'):#已经建立数据库
  93.         conn=connect('gamedetail.db')
  94.         curs=conn.cursor()
  95.     else:
  96.         conn=connect('gamedetail.db')
  97.         curs=conn.cursor()
  98.         curs.execute('create table gamelist(id integer primary key, name text, \
  99.                         type text, src text, tag text, width integer,\
  100.                         heigh text, score real, detail text)')
  101.     ps=ProgressSaver()#用于自动恢复计算
  102.     progress=ps.Load()
  103.     if progress=='':
  104.         progress=0
  105.     else:
  106.         progress=int(progress)+1
  107.     for x in range(progress,len(mu)):
  108.         a=GetGameInfo(mu[x])
  109.         while a==None: #访问失败,持续访问。
  110.             print "Connection Error! ReConnecting..."
  111.             a=GetGameInfo(mu[x])
  112.         try:
  113.             st='insert into gamelist values(%d,"%s","%s","%s","%s",%d,%d,%f,"%s")'%\
  114.                 (a.id,a.name,a.type,a.SRC(),a.tag,a.width,a.heigh,a.score,a.detail)
  115.             st=' '.join(st.split())#去除空白符和换行符
  116.             print x,st
  117.             st=unicode(st,'GBK')#进行编码转换,不然数据库里面会是乱码,用gb2312会有生僻字无法识别
  118.             curs.execute(st)
  119.             conn.commit()
  120.             ps.Save(str(x))
  121.         finally:
  122.             pass
  123.     conn.close()
  124. if __name__ == '__main__':
  125.     GetRawSeries()
  126.     #CreateDetailLib()
阅读(1735) | 评论(0) | 转发(0) |
0

上一篇:first

下一篇:code highlight test c#

给主人留下些什么吧!~~