test

    技术2026-08-10  3

    数据库MySQL 类

    # Aligned with opening delimiter foo = long_function_name(var_one, var_two, var_three, var_four) # More indentation included to distinguish this from the rest. def long_function_name( var_one, var_two, var_three, var_four): print(var_one) # Arguments on first line forbidden when not using vertical alignment foo = long_function_name(var_one, var_two, var_three, var_four) # Further indentation required as indentation is not distinguishable def long_function_name( var_one, var_two, var_three, var_four): print(var_one)

    # Aligned with opening delimiter foo = long_function_name(var_one, var_two, var_three, var_four) # More indentation included to distinguish this from the rest. def long_function_name( var_one, var_two, var_three, var_four): print(var_one) # Arguments on first line forbidden when not using vertical alignment foo = long_function_name(var_one, var_two, var_three, var_four) # Further indentation required as indentation is not distinguishable def long_function_name( var_one, var_two, var_three, var_four): print(var_one)  

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Created on 2011-2-19 # @author: penxiao import MySQLdb import sys __all__ = ['MySQL'] class MySQL(object): ''' MySQL ''' conn = '' cursor = '' def __init__(self,host='localhost',user='root',passwd='root',db='mysql',charset='utf8'): """MySQL Database initialization """ try: self.conn = MySQLdb.connect(host,user,passwd,db) except MySQLdb.Error,e: errormsg = 'Cannot connect to server/nERROR (%s): %s' %(e.args[0],e.args[1]) print errormsg sys.exit() self.cursor = self.conn.cursor() def query(self,sql): """ Execute SQL statement """ return self.cursor.execute(sql) def show(self): """ Return the results after executing SQL statement """ return self.cursor.fetchall() def __del__(self): """ Terminate the connection """ self.conn.close() self.cursor.close() 

     

    人人网相册下载代码:

    # coding utf-8 import sys import os import re import urllib import urllib2 import cookielib import cmd from lxml import html import cmd import json # import simplejson as json # < 2.6 class RRAD(): def __init__(self): # initialize the download dir. self.download_dir = 'albums' if not os.path.isdir(self.download_dir): os.mkdir(self.download_dir) # build the session self.session = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) # sign into renren.com def signin(self): username = raw_input('username: ') password = raw_input('password: ') data = ( ('email', username), ('password',password), ('origURL',"http://www.renren.com/Home.do"), ('domain',"renren.com")) print '%s is signing in...' % username page = self.session.open('http://www.renren.com/PLogin.do', urllib.urlencode(data)) page.close() print 'signin successful!' print '' # fetch source code by post def do_post(self, url): return self.session.open(url, {}).read() # fetch source code by get def do_get(self, url): return self.session.open(url).read() # get the album's photo links def get_photos(self, album_url): print 'analyzing album', album_url photo_links = [] # photo links in all pages curpage = 0 # page code while True: # fetching links page by page content = self.do_get(album_url + '?curpage=%d&t=#thumb' % curpage) doc = html.document_fromstring(content) links = doc.xpath('//div[@id="albumThumbMode"]//li//span[@class="img"]//a//@href') if links: photo_links.extend([re.sub(r'/?.*$', '', link) for link in links]) curpage = curpage + 1 # move to the next page print '%d photo(s) found in page %s' % (len(links), curpage) else: # no more pages print 'analyzing completed. %d photo(s) found in total.' % len(photo_links) break return photo_links # download the album def save_album(self, url): album_url = re.sub(r'[/?/#].*$', '', url) # create the album directory if not exists album_dir = os.path.join(self.download_dir, re.split('//', album_url)[-1]) if not os.path.isdir(album_dir): os.mkdir(album_dir) # fetch photos' links in the album links = self.get_photos(album_url) print '' # download each photo into the album directory print 'saving album to', album_dir for i, link in enumerate(links): print '(%d/%d) %s' % (i + 1, len(links), link), try: self.save_photo(album_dir, link) print 'saved.' except: print 'failed.' print 'all downloads completed.' # download the photo into the given album directory def save_photo(self, album_dir, url): # code = unicode(self.do_post(url + '/ajax'), 'raw_unicode_escape') # get details of the photo. photo = json.loads(self.do_post(url + '/ajax')) photo_url = photo.get('photo').get('large') filename = re.split('//', photo_url)[-1] # save the photo f = file(os.path.join(album_dir, filename), 'wb') f.write(self.session.open(photo_url).read()) f.close() class RRADCmd(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) self.intro = ''' Renren Album Downloader V0.1 ========================================= author : greatghoul email : greatghoul@gmail.com copyright : http://www.g2w.me ''' self.prompt = '> ' self.rrad = RRAD() def help_signin(self): print 'Sign into renren.com' def do_signin(self, null): self.rrad.signin() def help_save(self): print '''Save the given album. Example: save http://photo.renren.com/photo/253423487/album-396516481''' def do_save(self, album_url): self.rrad.save_album(album_url) def help_exit(self): print 'Quit the application.' def do_exit(self, null): sys.exit(0) if __name__ == '__main__': rrad_cmd = RRADCmd() rrad_cmd.cmdloop() # test_unicode raw_unicode_escapse 824

    最新回复(0)