`
shaojiashuai123456
  • 浏览: 256707 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
社区版块
存档分类
最新评论

python tornado 框架使用 (2)

 
阅读更多

(2)抓取系统

common_urllib.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys 
import os
import urllib
import urllib2
import traceback
import json
import logging
import types
import re
import common_logging

logger = logging.getLogger()

def get(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  

def post(url, dict_data={}):
    content = u'' 
    try:
        data = urllib.urlencode(dict_data)
        req = urllib2.Request(url, data)
        status = urllib2.urlopen(req)
        if status.getcode() == 200:
            content = status.read()
            content = unicode(content.strip(), 'utf-8', 'ignore')   
        else:
            logger.error('fetch error [%s]' % url)
    except:
        logger.error('%s' % traceback.format_exc())
    return content

def post_content(url, data):
    content = u''
    try:
        req = urllib2.Request(url, data)
        status = urllib2.urlopen(req)
        if status.getcode() == 200:
            content = status.read()
        else:
            logger.error('fetch error [%s]' % url)
    except:
        logger.error('%s' % traceback.format_exc())
    return content


if __name__ == '__main__':
    content = get("http://www.sina.com")
    print len(content)

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics