Skip to main content

Simplified python article discovery & extraction.

Project description

https://badge.fury.io/py/newspaper.png

Inspired by requests for its simplicity and powered by lxml for its speed:

“Newspaper is an amazing python library for extracting & curating articles.” – tweeted by Kenneth Reitz, Author of requests

“Newspaper delivers Instapaper style article extraction.” – The Changelog

Basic Demo: http://newspaper-demo.herokuapp.com

We support 10+ languages and everything is in unicode!

>>> import newspaper
>>> newspaper.languages()

Your available languages are:
input code      full name

  ar              Arabic
  ru              Russian
  nl              Dutch
  de              German
  en              English
  es              Spanish
  fr              French
  it              Italian
  ko              Korean
  no              Norwegian
  pt              Portuguese
  sv              Swedish
  hu              Hungarian
  fi              Finnish
  da              Danish
  zh              Chinese
  id              Indonesian
  vi              Vietnamese

A Glance:

>>> import newspaper

>>> cnn_paper = newspaper.build('http://cnn.com')

>>> for article in cnn_paper.articles:
>>>     print article.url
u'http://www.cnn.com/2013/11/27/justice/tucson-arizona-captive-girls/'
u'http://www.cnn.com/2013/12/11/us/texas-teen-dwi-wreck/index.html'
...

>>> for category in cnn_paper.category_urls():
>>>     print category

u'http://lifestyle.cnn.com'
u'http://cnn.com/world'
u'http://tech.cnn.com'
...
>>> article = cnn_paper.articles[0]
>>> article.download()

>>> article.html
u'<!DOCTYPE HTML><html itemscope itemtype="http://...'
>>> article.parse()

>>> article.authors
[u'Leigh Ann Caldwell', 'John Honway']

>>> article.text
u'Washington (CNN) -- Not everyone subscribes to a New Year's resolution...'

>>> article.top_image
u'http://someCDN.com/blah/blah/blah/file.png'

>>> article.movies
[u'http://youtube.com/path/to/link.com', ...]
>>> article.nlp()

>>> article.keywords
['New Years', 'resolution', ...]

>>> article.summary
u'The study shows that 93% of people ...'

Newspaper has seamless language extraction and detection. If no language is specified, Newspaper will attempt to auto detect a language.

>>> from newspaper import Article
>>> url = 'http://www.bbc.co.uk/zhongwen/simp/chinese_news/2012/12/121210_hongkong_politics.shtml'

>>> a = Article(url, language='zh') # Chinese

>>> a.download()
>>> a.parse()

>>> print a.text[:150]
香港行政长官梁振英在各方压力下就其大宅的违章建
筑(僭建)问题到立法会接受质询,并向香港民众道歉。
梁振英在星期二(12月10日)的答问大会开始之际
在其演说中道歉,但强调他在违章建筑问题上没有隐瞒的
意图和动机。 一些亲北京阵营议员欢迎梁振英道歉,
且认为应能获得香港民众接受,但这些议员也质问梁振英有

>>> print a.title
港特首梁振英就住宅违建事件道歉

If you are certain that an entire news source is in one language, go ahead and use the same api :)

>>> import newspaper
>>> sina_paper = newspaper.build('http://www.sina.com.cn/', language='zh')

>>> for category in sina_paper.category_urls():
>>>     print category
u'http://health.sina.com.cn'
u'http://eladies.sina.com.cn'
u'http://english.sina.com'
...

>>> article = sina_paper.articles[0]
>>> article.download()
>>> article.parse()

>>> print article.text
新浪武汉汽车综合 随着汽车市场的日趋成熟,
传统的“集全家之力抱得爱车归”的全额购车模式已然过时,
另一种轻松的新兴 车模式――金融购车正逐步成为时下消费者购
买爱车最为时尚的消费理念,他们认为,这种新颖的购车
模式既能在短期内
...

>>> print article.title
两年双免0手续0利率 科鲁兹掀背金融轻松购_武汉车市_武汉汽
车网_新浪汽车_新浪网

Documentation

Check out The Documentation for full and detailed guides using newspaper.

Interested in adding a new language for us? Refer to: Docs - Adding new languages

Features

  • Works in 10+ languages (English, Chinese, German, Arabic, …)

  • Multi-threaded article download framework

  • News url identification

  • Text extraction from html

  • Top image extraction from html

  • All image extraction from html

  • Keyword extraction from text

  • Summary extraction from text

  • Author extraction from text

  • Google trending terms extraction

Get it now

Installing newspaper is simple with pip. However, you will run into fixable issues if you are trying to install on ubuntu.

If you are on Debian / Ubuntu, install using the following:

  • Python development version, needed for Python.h:

    $ sudo apt-get install python-dev
  • lxml requirements:

    $ sudo apt-get install libxml2-dev libxslt-dev
  • For PIL to recognize .jpg images:

    $ sudo apt-get install libjpeg-dev zlib1g-dev libpng12-dev
  • Install the distribution via pip:

    $ pip install newspaper
  • Download NLP related corpora:

    $ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python2.7

If you are on OSX, install using the following, you may use both homebrew or macports:

$ brew install libxml2 libxslt

$ brew install libtiff libjpeg webp little-cms2

$ pip install newspaper

$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python2.7

Otherwise, install with the following:

NOTE: You will still most likely need to install the following libraries via your package manager

  • PIL: libjpeg-dev zlib1g-dev libpng12-dev

  • lxml: libxml2-dev libxslt-dev

  • Python Development version: python-dev

$ pip install newspaper

$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python2.7

Development

If you’d like to contribute and hack on the newspaper project, feel free to clone a development version of this repository locally:

git clone git://github.com/codelucas/newspaper.git

Once you have a copy of the source, you can embed it in your Python package, or install it into your site-packages easily:

$ pip install -r requirements.txt
$ python setup.py install

Feel free to give our testing suite a shot:

$ python tests/unit_tests.py

LICENSE

Authored and maintained by Lucas Ou-Yang.

Newspaper uses a lot of python-goose’s parsing code. View their license here.

Please feel free to email & contact me if you run into issues or just would like to talk about the future of this library and news extraction in general!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

newspaper-0.0.9.tar.gz (760.2 kB view details)

Uploaded Source

Built Distribution

newspaper-0.0.9.macosx-10.6-x86_64.exe (7.2 MB view details)

Uploaded Source

File details

Details for the file newspaper-0.0.9.tar.gz.

File metadata

  • Download URL: newspaper-0.0.9.tar.gz
  • Upload date:
  • Size: 760.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for newspaper-0.0.9.tar.gz
Algorithm Hash digest
SHA256 eec04570355e5af9b042c88e5b4d5124ac4f5e645b744c7964b1dd090e581b68
MD5 15ebb599b02edbd12d09783f3c6e4894
BLAKE2b-256 b12d29d086b2db39b3f4e284411fe65317e6d645e575e236fd9cda08f80b1c3a

See more details on using hashes here.

File details

Details for the file newspaper-0.0.9.macosx-10.6-x86_64.exe.

File metadata

File hashes

Hashes for newspaper-0.0.9.macosx-10.6-x86_64.exe
Algorithm Hash digest
SHA256 2307991a869df578ac8d75a33aaf5919578f94ede3be46b68883dab1e7623933
MD5 8af8ec67d52f70475d748757d7eba3d2
BLAKE2b-256 52c712e1398b937bcbb1d51ece591619bb2b7b24b2e7d31ccb3c553fab20cd2a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page