Skip to content

Added option to scrape page for attributes in case og meta elements are not present #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions opengraph/opengraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
class OpenGraph(dict):
"""
"""
def __init__(self, url=None, html=None, **kwargs):

required_attrs = ['title', 'type', 'image', 'url']
scrape = False

def __init__(self, url=None, html=None, scrape=False, **kwargs):
# If scrape == True, then will try to fetch missing attribtues
# from the page's body
self.scrape = scrape
self._url = url

for k in kwargs.keys():
self[k] = kwargs[k]

Expand Down Expand Up @@ -49,14 +58,18 @@ def parser(self, html):
ogs = doc.html.head.findAll(property=re.compile(r'^og'))
for og in ogs:
self[og[u'property'][3:]]=og[u'content']

# Couldn't fetch all attrs from og tags, try scraping body
if not self.is_valid() and self.scrape:
for attr in self.required_attrs:
if not hasattr(self, attr):
try:
self[attr] = getattr(self, 'scrape_%s' % attr)(doc)
except AttributeError:
pass

def is_valid(self):
if hasattr(self,'title') and \
hasattr(self,'type') and \
hasattr(self,'image') and \
hasattr(self,'url'):

return True
return all([hasattr(self, attr) for attr in self.required_attrs])

def to_html(self):
if not self.is_valid():
Expand All @@ -82,3 +95,21 @@ def to_json(self):

def to_xml(self):
pass

def scrape_image(self, doc):
images = [dict(img.attrs)['src']
for img in doc.html.body.findAll('img')]

if images:
return images[0]

return u''

def scrape_title(self, doc):
return doc.html.head.title.text

def scrape_type(self, doc):
return 'other'

def scrape_url(self, doc):
return self._url