posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          燕曦版友信息統計腳本

          Posted on 2007-09-10 12:27 ZelluX 閱讀(421) 評論(2)  編輯  收藏 所屬分類: Scripting
          connector.py
          import urllib, urllib2, cookielib

          class MyConnector:
              
          def __init__(self):
                  
          pass
              
              
          def login(self, url):
                  cookie 
          = cookielib.CookieJar()
                  opener 
          = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
                  urllib2.install_opener(opener)
                  str 
          = urllib.urlencode({'id''guest''passwd'''})
                  self.sock 
          = urllib2.urlopen(url, str)
              
              
          def getHTML(self, url):
                  self.sock 
          = urllib2.urlopen(url)
                  
          return self.sock.read()

          yanxiparser.py
          from sgmllib import SGMLParser
          import re

          class YanxiURLParser(SGMLParser):
              
          def reset(self):
                  self.result 
          = []
                  SGMLParser.reset(self)
              
              
          def start_a(self, attrs):
                  
          for (k, v) in attrs:
                      
          if (k == 'href' and (v.find('bbsanc'>= 0)):
                          self.result.append(v)
                          
          class YanxiHTMLParser:
              
          def parse(self, html):
                  uid 
          = ufrom = ubirth = ufav = ''
                  
                  html 
          = html.replace(r' '' ')
                  html 
          = html.replace(r'<br />''')
                  
                  pattern 
          = '\xbe\xcd\xca\xc7(.*)\xc0\xb2'
                  matchObject 
          = re.search(pattern, html)
                  uid 
          = matchObject.group(1)
                  uid 
          = uid.strip()
                  
                  pattern 
          = '\xc0\xb4\xd7\xd4(.*)\xa3(\xac|xa1)'
                  matchObject 
          = re.search(pattern, html)
                  ufrom 
          = matchObject.group(1)
                  ufrom 
          = ufrom.strip()
                  
                  pattern 
          = '\xcf\xb2\xbb\xb6(.*)\n'
                  matchObject 
          = re.search(pattern, html)
                  ufav 
          = matchObject.group(1)
                  ufav 
          = ufav.strip()
                  
                  pattern 
          = '\n(.*)\xca\xc7\xce\xd2\xb5\xc4\xc9\xfa\xc8\xd5'
                  matchObject 
          = re.search(pattern, html)
                  ubirth 
          = matchObject.group(1)
                  ubirth 
          = ubirth.strip()
                  
          return {"id" : uid, "from" : ufrom, "birth" : ubirth, "fav" : ufav}

          runner.py
          from connector import MyConnector
          from yanxiparser import *

          rootURL 
          = 'http://yanxibbs.cn'
          loginURL 
          = 'http://yanxibbs.cn/bbslogin.php'
          url1 
          = 'http://yanxibbs.cn/cgi-bin/bbs/bbs0an?path=%2Fgroups%2FGROUP%5F3%2F06SS%2Fbyxx%2Fbjcy'
          url2 
          = 'http://yanxibbs.cn/cgi-bin/bbs/bbs0an?path=%2Fgroups%2FGROUP%5F3%2F06SS%2Fbyxx%2Fbjyr'

          conn 
          = MyConnector()
          conn.login(loginURL)

          def printInfo(url):
              html 
          = conn.getHTML(url)
              urlParser 
          = YanxiURLParser()
              htmlParser 
          = YanxiHTMLParser()
              urlParser.feed(html)
              
              
          for targetURL in urlParser.result:
                  html 
          = conn.getHTML(rootURL + targetURL)
                  info 
          = htmlParser.parse(html)
                  
          print "%(id)s\t%(from)s\t%(birth)s\t%(fav)s" % info
              
          printInfo(url1)
          printInfo(url2)

          評論

          # re: 燕曦版友信息統計腳本  回復  更多評論   

          2009-01-13 02:16 by SmartQ
          天哪你是。。。

          # re: 燕曦版友信息統計腳本  回復  更多評論   

          2009-01-13 09:48 by ZelluX
          @SmartQ
          ZelluX@yanxi
          主站蜘蛛池模板: 九寨沟县| 汉川市| 建平县| 甘孜| 舟山市| 于田县| 礼泉县| 贺州市| 六枝特区| 尼木县| 雷山县| 泗水县| 浙江省| 正定县| 台山市| 新疆| 昭苏县| 菏泽市| 乌鲁木齐县| 盐津县| 塔河县| 沂南县| 墨脱县| 四子王旗| 原阳县| 郑州市| 武陟县| 沂南县| 洛宁县| 潼南县| 白玉县| 小金县| 库车县| 景洪市| 渝中区| 四会市| 象山县| 汽车| 合江县| 开封县| 晋城|