Python库世界收集

库名: MySQLdb
简介:python下操作mysql数据库
地址:http://sourceforge.net/projects/mysql-python/
安装:

或者自己下载
http://sourceforge.net/projects/mysql-python/files/latest/download

使用:

#===============================================================================
# -*- coding: utf-8 -*-
#MySQLdb封装类
#author:paul wang
#===============================================================================

import MySQLdb as mdb

class myMySQL:

    def connect(self,host="localhost",user="root",pwd="",database="",autocommit=False):
        try:
            self.isConnect = False

            self.conn = mdb.connect( host, user,
                pwd, database);

            self.isConnect = True

            self.cursor = self.conn.cursor()
            self.cursor.execute("SELECT VERSION()")

            data = self.cursor.fetchone()

            if autocommit:
                self.conn.autocommit(True)
            else:
                self.conn.autocommit(False)

        except mdb.Error as e:
            print ( "Connect Error %d: %s" % (e.args[0],e.args[1]) )

        print ( "Database version : %s " % data )

    def close(self):
        try:
            self.cursor.close()
            self.conn.close()
        except mdb.Error as e:
            print ( "Close Error %d: %s" % (e.args[0],e.args[1]) )

    def excute(self,sql=""):
        try:
            self.cursor.execute(sql)
        except mdb.Error as e:
            print ( "Excute Error %d: %s" % (e.args[0],e.args[1]) )
            print ( "Excute sql= %s" % sql )

    def getrows(self,sql):
        try:
            self.excute(sql)
            rows = self.cursor.fetchall()
            return rows
        except mdb.Error as e:
            print ( "getrows Error %d: %s" % (e.args[0],e.args[1]) )   

    def selectDB(self,dbName):
        self.conn.select_db(dbName)

    def commit(self):
        self.conn.commit()

    def rollback(self):
        self.conn.rollback()

    def setautocommit(self,auto=False):
        self.conn.autocommit(auto)

    def isConnected(self):
        return self.isConnect

#下面是测试代码
#db = myMySQL()
#db.connect( "localhost","root","","drupal",False )
#db.setautocommit(False)
#db.excute("insert into test values(2)")
#db.rollback()
#rows = db.getrows("select * from test where 1 = 1 ")
#print( rows )
#db.close()

库名:hashlib
简介:hashlib是个专门提供hash算法的库,现在里面包括md5, sha1, sha224, sha256, sha384, sha512,使用非常简单、方便。 md5经常用来做用户密码的存储。而sha1则经常用作数字签名。
地址:内置库
安装:默认已经安装在python2.7中
使用:

>>> import hashlib
>>> m = hashlib.md5()
>>> m.update("Nobody inspects")
>>> m.update(" the spammish repetition")
>>> m.digest()
'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
>>> m.digest_size
16
>>> m.block_size
64


库名:urllib,urllib2
简介:Python的一个获取URLs(Uniform Resource Locators)的组件。他以urlopen函数的形式提供了一个非常简单的接口,具有利用不同协议获取URLs的能力,提供了一个比较复杂的接口来处理一般情况,例如:基础验证,cookies,代理等。
地址:内置库
安装:默认已经安装在python2.7中
使用:

#! /usr/bin/env python

# -*- coding=utf-8 -*-
# @Author pythontab
import urllib
url = "http://www.youkuaiyun.com"
html = urllib.urlopen(url).read()
print(html)

库名:Beautiful Soup
简介:
Beautiful Soup 库是一个非常神奇的 “粗糙的解析器”,用于解析实际 Web 页面中包含的有效 HTML。
地址:内置库
安装:easy_install BeautifulSoup
使用:

import urllib2
from BeautifulSoup import BeautifulSoup
import time

page = urllib2.urlopen("www.baidu.com")
soup = BeautifulSoup(page)
timenow = time.strftime("%Y-%m-%d %X",time.localtime())
print timenow,soup.findAll('b')[2].contents[0]


待续


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值