Skip to content
Open
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
11 changes: 6 additions & 5 deletions opengraph/opengraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class OpenGraph(dict):

required_attrs = ['title', 'type', 'image', 'url', 'description']

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

Expand All @@ -33,7 +33,7 @@ def __init__(self, url=None, html=None, scrape=False, **kwargs):
dict.__init__(self)

if url is not None:
self.fetch(url)
self.fetch(url, headers)

if html is not None:
self.parser(html)
Expand All @@ -44,10 +44,11 @@ def __setattr__(self, name, val):
def __getattr__(self, name):
return self[name]

def fetch(self, url):
def fetch(self, url, headers):
"""
"""
raw = urllib2.urlopen(url)
req = urllib2.Request(url, headers=headers)
raw = urllib2.urlopen(req)
html = raw.read()
return self.parser(html)

Expand Down Expand Up @@ -123,4 +124,4 @@ def scrape_url(self, doc):
def scrape_description(self, doc):
tag = doc.html.head.findAll('meta', attrs={"name":"description"})
result = "".join([t['content'] for t in tag])
return result
return result