Skip to content
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

Use proper request headers when opening URI. #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def download_jars(datapath, version=boilerpipe_version):
tgz_url = 'https://boilerpipe.googlecode.com/files/boilerpipe-{0}-bin.tar.gz'.format(version)
tgz_url = 'https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/boilerpipe/boilerpipe-{0}-bin.tar.gz'.format(boilerpipe_version)
tgz_name = basename(tgz_url)
if not exists(tgz_name):
urlretrieve(tgz_url, tgz_name)
Expand Down
36 changes: 29 additions & 7 deletions src/boilerpipe/extract/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,39 @@ class Extractor(object):
extractor = None
source = None
data = None
headers = {'User-Agent': 'Mozilla/5.0'}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Connection": "keep-alive"
}

def __init__(self, extractor='DefaultExtractor', **kwargs):

if kwargs.get('logger'):
self.logger = kwargs['logger']
else:
self.logger = None

if kwargs.get('url'):
request = urllib2.Request(kwargs['url'], headers=self.headers)
connection = urllib2.urlopen(request)
self.data = connection.read()
encoding = connection.headers['content-type'].lower().split('charset=')[-1]
if encoding.lower() == 'text/html':
encoding = charade.detect(self.data)['encoding']
self.data = unicode(self.data, encoding)
try:
connection = urllib2.urlopen(request)
except:
connection = None
if self.logger is not None:
self.logger.exception( 'boilerpipe extractor failed on urlopen() for uri %s' % kwargs['url'] )

if connection is not None:
self.data = connection.read()
encoding = connection.headers['content-type'].lower().split('charset=')[-1]
if encoding.lower() == 'text/html':
encoding = charade.detect(self.data)['encoding']
self.data = unicode(self.data, encoding)
else:
if self.logger is not None:
self.logger.debug('boilerpipe execution continues with empty document')
self.data = u''

elif kwargs.get('html'):
self.data = kwargs['html']
if not isinstance(self.data, unicode):
Expand Down