Skip to content

Instantly share code, notes, and snippets.

@quasiben
Created June 30, 2014 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quasiben/8f42cc3152c9c0394866 to your computer and use it in GitHub Desktop.
Save quasiben/8f42cc3152c9c0394866 to your computer and use it in GitHub Desktop.
python_postgres_frankenlanguage
CREATE OR REPLACE FUNCTION bz_select_html(param_search text)
RETURNS SETOF text AS
$$
from lxml import html
import requests
page = requests.get('http://econpy.pythonanywhere.com/ex/001.html')
param = param_search.lower()
tree = html.fromstring(page.text)
if param == 'buyers':
results = tree.xpath('//div[@title="buyer-name"]/text()')
elif param == 'prices':
results = tree.xpath('//span[@class="item-price"]/text()')
else:
results = []
return results
$$
LANGUAGE 'plpython2u' VOLATILE SECURITY DEFINER;
SELECT search_term, bz_select_html(search_term) As
result FROM (VALUES ('buyers')) As X(search_term);
@quasiben
Copy link
Author

results

 search_term |         result         
-------------+------------------------
 buyers      | Carson Busses
 buyers      | Earl E. Byrd
 buyers      | Patty Cakes
 buyers      | Derri Anne Connecticut
 buyers      | Moe Dess
 buyers      | Leda Doggslife
 buyers      | Dan Druff
 buyers      | Al Fresco
 buyers      | Ido Hoe
 buyers      | Howie Kisses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment