Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Created February 1, 2013 10:33
Show Gist options
  • Save v2e4lisp/4690553 to your computer and use it in GitHub Desktop.
Save v2e4lisp/4690553 to your computer and use it in GitHub Desktop.
php-lookup: find examples of php functions from official website
import requests as rq
from pyquery import PyQuery as pq
# helper
def unescape(string):
import re
entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
def substitute_entity(match):
from htmlentitydefs import name2codepoint as n2cp
ent = match.group(2)
if match.group(1) == "#":
return unichr(int(ent))
else:
cp = n2cp.get(ent)
if cp:
return unichr(cp)
else:
return match.group()
return entity_re.subn(substitute_entity, string)[0]
def php_get_page (keyword):
url = 'http://www.php.net/manual/ja/function.array-filter.php'
html = pq(rq.get(url).text)
egs = html.find('div.example-contents')
return php_get_egs(egs)
def php_get_egs(egs):
if not len(egs) % 3 == 0: raise SystemExit
groups = [egs[x:x+3] for x in xrange(0, len(egs), 3)]
return [php_get_eg(g) for g in groups]
def php_get_eg(eg):
if not len(eg) == 3: raise SystemExit
return (php_get_code(eg), php_get_output(eg))
def php_get_code(eg):
return pq(eg[0])("div.phpcode").text()
# spans = pq(eg[0])("div.phpcode")("span")
def php_get_output(eg):
return pq(eg[2])("div.cdata").text()
r = php_get_page('hh')
for i in r:
print i[0]
print "output > "
print i[1]
print "-."*40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment