常用方法

(1)取文件路径

 

from unipath import Path

ofn = 'test.txt'
self.cwd = Path(__file__).ancestor(1)
ofn_path = Path(self.cwd, ofn)

 

(2)打印错误堆栈信息

import traceback

logger.error('error [%s]'% (traceback.format_exc()))

 

(3)测试方法用时

from timeit import Timer
t1=Timer("test()","from __main__ import test")
print t1.timeit(10000)
print min(t1.repeat(3,10000))

 (3)测试内存回收和使用

import gc , objgraph 

gc.collect()
objgraph.show_most_common_types(limit=50)

 (4) 测试用时

  

before = datetime.datetime.now()
。。。
end = datetime.datetime.now()
logger.error('init [%s]'% (end - before))

  (5) 网页抓取

     import urllib
     def fetch(url):
        #logger.error('fetch:%s' % url)
        content = u''
        try:
            status = urllib.urlopen(url)
            if status.getcode() == 200:
                content = status.read()
                content = unicode(content.strip(), 'utf-8', 'ignore')
            else:
                logger.error('fetch error [%s]' % url)
        except:
            logger.error('fetch error %s' % traceback.format_exc())
      return content

  (6) 网页解析

  import re  
  def parse_html(self, url, content):
        # logger.error('url[%s][%s]' % (url,content))
        title_pattern = r'<ul class="movie_list">(?P<detail_content>.*?)</ul>'
        detail_pattern = r'<li>.*?href="(?P<detail_url>.*?)".*?title="(?P<detail_title>.*?)">.*?</li>'
        list_res = []
        res = re.search(title_pattern,content,re.S)
        if res:
            detail_content = res.group('detail_content')
            #logger.error('parse_html:%s' % detail_content)
            list_res = re.findall(detail_pattern, detail_content, re.S)
            if not list_res:
                list_res = []

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值