Created
September 9, 2013 20:53
-
-
Save georgemarshall/6501354 to your computer and use it in GitHub Desktop.
HTML to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from bs4 import BeautifulSoup | |
import json | |
data = {'photos': []} | |
soup = BeautifulSoup(open('gallery.html')) | |
for item in soup.find_all('li'): | |
if 'testimony' in item['class']: | |
continue | |
obj = { | |
'description': item.a['data-description'], | |
'featured': 'box-6' in item['class'], | |
'href': item.a['href'], | |
'img': item.a.img['src'], | |
'tags': [tag for tag in item['class'] if tag not in ('box-3', 'box-6', 'is-loading')], | |
'title': item.a['title'] | |
} | |
data['photos'].append(obj) | |
with open('gallery.json', 'w') as fp: | |
json.dump(data, fp, sort_keys=True, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment