Skip to content

Instantly share code, notes, and snippets.

@kaiquewdev
Created April 6, 2012 05:47
Show Gist options
  • Select an option

  • Save kaiquewdev/2317390 to your computer and use it in GitHub Desktop.

Select an option

Save kaiquewdev/2317390 to your computer and use it in GitHub Desktop.
Get a value of a place in web page
#!/usr/bin/env python
# coding: utf-8
class Exchange( object ):
def __init__( self, url ):
'''
External Dependencies: Pycurl and BeautifulSoup
'''
output = False
try:
import pycurl
import StringIO
self.curl = pycurl
self.StringIO = StringIO
self.url = url
self.soup = ''
self.soupJSON = ''
self._cobweb = ''
except ImportError:
return output
def getContentUrl( self ):
'''
Go to url and get content
'''
output = False
try:
curl = self.curl
StringIO = self.StringIO
url = self.url
if url:
cinit = curl.Curl()
cinit.setopt( curl.URL, url )
cinit.setopt( curl.HTTPHEADER, ['Accepted:'] )
IO = StringIO.StringIO()
cinit.setopt( curl.WRITEFUNCTION, IO.write )
cinit.setopt( curl.FOLLOWLOCATION, 1 )
cinit.setopt( curl.MAXREDIRS, 5 )
cinit.perform()
output = IO.getvalue()
return output
except Exception:
return output
def setSoup( self ):
'''
Set soup in the instance
>>> Exchange('www.google.com').setSoup()
True
>>> Exchange('').setSoup()
False
'''
output = False
try:
import BeautifulSoup
contentUrl = self.getContentUrl()
soup = BeautifulSoup.BeautifulSoup( contentUrl )
if soup:
self.soup = soup
output = True
return output
except Exception:
return output
except ImportError:
return output
def getSoup( self ):
'''
Make a soup of content was setted
'''
output = False
try:
if self.soup:
output = self.soup
elif not self.soup:
if self.setSoup():
if self.soup:
output = self.soup
return output
except Exception:
return output
def getJSON( self ):
'''
Make a JSON soup decoded
'''
output = False
try:
import json
if self.soup or not self.soup:
if self.setSoup() and self.soupJSON or not self.soupJSON:
self.soupJSON = json.loads( self.soup.string )
if self.soupJSON:
output = True
return output
except Exception:
return output
except ImportError:
return output
def getIdentifier( self, pattern='' ):
'''
Select using the pattern of tag, tag#id or tag.class
'''
# Identifier tag and attribute
output = False
try:
# Html tags and html attributes identifiers
ids = {
'id': '#',
'class': '.',
}
if pattern:
if ids['id'] in pattern:
output = pattern.split( ids['id'] )
if ids['class'] in pattern:
output = pattern.split( ids['class'] )
else:
output = [ pattern ]
return output
except Exception:
return output
def getSpidering( self, pattern='' ):
'''
Surround the content of url into the mark pattern
>>> emptyTest = Exchange('')
>>> emptyTest.getSpidering()
False
>>> notEmptyTest = Exchange('http://economia.uol.com.br/cotacoes/cambio.jhtm')
>>> notEmptyTest.getSpidering('div.col-mod1')
True
>>> notEmptyTest = Exchange('http://economia.uol.com.br/cotacoes/cambio.jhtm')
>>> notEmptyTest.getSpidering('div.col-mod1>table>tbody')
True
'''
output = False
try:
if pattern:
if self.setSoup():
soup = self.soup
pattern = pattern.split('>')
cobweb = ''
for topLevel in pattern:
level = self.getIdentifier( topLevel )
if not cobweb:
if len( level ) == 1:
cobweb = soup.find( level[0] )
if len( level ) > 1:
cobweb = soup.find( level[0], level[1] )
self._cobweb = cobweb
if self._cobweb:
output = True
return output
except Exception:
return output
def getChilds( self, pattern='' ):
'''
Get deep selection
'''
output = False
try:
if pattern:
if self._cobweb:
if len( self.getIdentifier( pattern ) ) == 1:
output = self._cobweb
output = output.findAll( pattern )
return output
except Exception:
return output
if __name__ == '__main__':
import doctest; doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment