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

added getTitle function #15

Open
wants to merge 3 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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

boilerpipe-1.2.0-bin.tar.gz

*.jar

build/lib/boilerpipe/__init__.py

build/lib/boilerpipe/extract/__init__.py
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ Then, to extract relevant content:

extracted_html = extractor.getHTML()

extracted_title = extractor.getTitle()

.. _Boilerpipe: http://code.google.com/p/boilerpipe/
17 changes: 8 additions & 9 deletions src/boilerpipe/extract/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jpype
import urllib2
import requests
import socket
import charade
import threading
Expand Down Expand Up @@ -29,16 +29,12 @@ class Extractor(object):
source = None
data = None
headers = {'User-Agent': 'Mozilla/5.0'}

def __init__(self, extractor='DefaultExtractor', **kwargs):
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)
response = requests.request('GET', kwargs['url'], headers=self.headers, timeout=10)
self.data = response.text

elif kwargs.get('html'):
self.data = kwargs['html']
if not isinstance(self.data, unicode):
Expand Down Expand Up @@ -68,6 +64,9 @@ def getText(self):
def getHTML(self):
highlighter = HTMLHighlighter.newExtractingInstance()
return highlighter.process(self.source, self.data)

def getTitle(self):
return self.source.getTitle()

def getImages(self):
extractor = jpype.JClass(
Expand Down