extract_urls.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:CommonScripts 作者: kushao1267 项目源码 文件源码
def saved(res, database='1', db_name='test', username='root', passwd='a8416459'):  # ??????,mysql?mongodb
    if database == '1':
        try:
            conn = MySQLdb.Connection(
                user=username, passwd=passwd, db=db_name, use_unicode=True)
            cursor = conn.cursor()  # ??cursor
            cursor.execute('drop table urls')  # ??????varchar(10)
            cursor.execute(
                'create table if not exists urls(ID tinyint primary key,URL varchar(50),CTIME timestamp not null default current_timestamp)')
            k = 0
            for i in res:
                k += 1
                # ???%s?????,???int??
                cursor.execute(
                    "insert into urls (ID,URL) values (%s,%s)", [str(k), i])
            cursor.execute('select * from urls')
            return True
        except Exception, e:
            print e
            return False
        finally:
            cursor.close()
            conn.close()
    else:  # ??mongodb
        ISOTIMEFORMAT = '%Y-%m-%d %X'
        try:
            conn = pymongo.MongoClient(host='localhost', port=27017)
            db = conn[db_name]
            coll = db['urls']
            # coll.drop()     #???????url??
            coll.remove()  # ??url??????
            for i in res:
                coll.insert({'URL': i, 'CTIME': time.strftime(
                    ISOTIMEFORMAT, time.localtime(time.time()))})
        except Exception, e:
            print e
        finally:
            conn.close()
        return True
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号