Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 4.68 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import urllib2, re, os, logging, unicodedata
  2. from BeautifulSoup import BeautifulSoup
  3. #import MySQLdb
  4.  
  5. class PHP:
  6.     def __init__(self):
  7.         soup = BeautifulSoup(urllib2.urlopen('http://php.net/quickref.php'))
  8.     def mineData(self,what):
  9. #db = MySQLdb.connect("localhost","root","","api")
  10. #c = db.cursor()
  11.         logger = logging.getLogger('mine')
  12.         hdlr = logging.FileHandler('mine.log')
  13.         formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
  14.         hdlr.setFormatter(formatter)
  15.         logger.addHandler(hdlr)
  16.         logger.setLevel(logging.WARNING)
  17.  
  18.         soup = BeautifulSoup(urllib2.urlopen('http://php.net/quickref.php'))
  19.         for x in soup.find('tr', valign='top').findAll('a'):
  20.             if re.compile('/manual/en/'+what).match(x['href']):
  21.                 url = 'http://php.net/' + x['href']
  22.                 print url
  23.                 fun = BeautifulSoup(urllib2.urlopen(url))
  24.                 try:
  25.                     # Function Name
  26.                     kind =""
  27.                     name =  fun.find('h1', 'refname').find(text=True).strip()
  28.                     # Function Kind
  29.                     if (re.search("::" , name)):
  30.                         kind = 'method'
  31.                 except AttributeError:
  32.                     logger.error("Function Name: " + url)
  33.                     continue
  34.                 try:
  35.                     # PHP Version
  36.                     version = fun.find('p', 'verinfo').find(text=True).strip()
  37.                 except AttributeError:
  38.                     logger.error('PHP Version: ' + url)
  39.                     continue
  40.                 try:
  41.                     # Long Description
  42.                     lDescription = ''
  43.                     if fun.find('div', 'description'):
  44.                         for x in fun.find('div', 'description').findAll('p', {'class' : re.compile("^(|sim)para")}):
  45.                             for y in x.findAll(text=True):
  46.                                 lDescription = lDescription + y
  47.                     else:
  48.                         lDescription = None
  49.                 except AttributeError:
  50.                     logger.error('Long Description: ' + url)
  51.                     continue
  52.                 try:
  53.                                 # Return Type of Function
  54.                     if(fun.find('div', 'methodsynopsis')):
  55.                         rType = fun.find('div', 'methodsynopsis').span.find(text=True).strip()
  56.                     else:
  57.                         rType = None
  58.                 except AttributeError:
  59.                     logger.error('Function Return Type: ' + url)
  60.                     continue
  61.                 try:
  62.                     # Return Value Description of Function
  63.                     if(fun.find('div', 'returnvalues')):
  64.                         returnValueDescription = re.sub("\s+", " ", ''.join(fun.find('div', 'returnvalues').find('p', 'para').findAll(text=True)).replace('\n', '')).strip()
  65.                     else:
  66.                         returnValueDescription = None
  67.                 except AttributeError:
  68.                     logger.error('Return Value Description: ' + url)
  69.                     continue
  70.                 try:
  71.                     # Short Description
  72.                     sDescription = re.sub("\s+", " ", ''.join(fun.find('p', 'refpurpose').findAll(text=True)).replace('\n', '')).strip()
  73.                 except AttributeError:
  74.                     logger.error("Short Description: " + url)
  75.                     continue
  76.                 try:
  77.                     # Parameters
  78.                     if fun.find('span', 'methodparam').find(text=True) == 'void':
  79.                         param = 'void'
  80.                     elif fun.find('span', 'methodparam').find(text=True) != 'void':
  81.                         #[[x.find(('span', 'type')).find(text=True), x.find(('tt', 'parameter')).find(text=True)] for x in fun.findAll('span',      'methodparam')]                
  82.                         para = ''
  83.                         for x in fun.findAll('span', 'methodparam'):
  84.                             para += x.find(('span', 'type')).find(text=True)+' '+x.find(('tt', 'parameter')).find(text=True)+', '
  85.                         param =  para.rstrip(', ')
  86.                     else:
  87.                         param = None
  88.                 except AttributeError:
  89.                     logger.error("Parameters: " + url)
  90.                     continue
  91.                 #try:
  92.                 #    c.execute('insert into phpFunctions values("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")' % (name, version, lDescription, rType,     returnValueDescription, sDescription, param, url))
  93.         #except:
  94.         #    report("DataBase Insert: " + url)
  95.         #    continue
  96.         return