Skip to content

Commit

Permalink
Adds user_agent kwarg to prevent 403 forbidden errors. Borrowed via i…
Browse files Browse the repository at this point in the history
…ssue erikriver#22.
  • Loading branch information
chadpaulson committed Aug 27, 2017
1 parent 09c464f commit c83fd04
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 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, html=None, scrape=False, user_agent=None, **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, user_agent)

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

def fetch(self, url):
def fetch(self, url, user_agent):
"""
"""
raw = urllib2.urlopen(url)
req = urllib2.Request(url)
if user_agent:
req.add_header('User-agent', user_agent)
raw = urllib2.urlopen(req)
html = raw.read()
return self.parser(html)

Expand Down

0 comments on commit c83fd04

Please sign in to comment.