Skip to content

Instantly share code, notes, and snippets.

@max747
Created October 5, 2012 14:34
Show Gist options
  • Select an option

  • Save max747/3840122 to your computer and use it in GitHub Desktop.

Select an option

Save max747/3840122 to your computer and use it in GitHub Desktop.
BeautifulScrap
#!/usr/bin/env python
from __future__ import print_function
from BeautifulSoup import BeautifulSoup
def scrape(src, expr):
soup = BeautifulSoup(src)
expr_stack = expr.split(".")
scrap = _scrape(soup, expr_stack)
print(scrap)
def _scrape(soup, expr_stack):
_soup = soup.findAll(expr_stack[0])
for token in expr_stack[1:]:
if token.isdigit():
_soup = _soup.__getitem__(int(token))
elif hasattr(_soup, "__getslice__"):
_soup = _soup.__getitem__(0)
else:
_soup = getattr(_soup, token)
return _soup
if __name__ == "__main__":
import sys
expr = sys.argv[1]
src = sys.stdin.read()
scrape(src, expr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment