From 60c7853fe24afa58da3ad1f994f5bf387c4ce429 Mon Sep 17 00:00:00 2001 From: El Aich Marouane Date: Fri, 20 Nov 2015 20:34:44 +0000 Subject: [PATCH] Adding a headers parameter --- opengraph/opengraph.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/opengraph/opengraph.py b/opengraph/opengraph.py index 9cedb97..7e7c9bb 100644 --- a/opengraph/opengraph.py +++ b/opengraph/opengraph.py @@ -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 @@ -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) @@ -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) @@ -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 \ No newline at end of file + return result