newspaper
newspaper3k is a news, full-text, and article metadata extraction in Python 3. Advanced docs:
Top Related Projects
news-please - an integrated web crawler and information extractor for news that just works
fast python port of arc90's readability tool, updated to match latest readability.js!
Html Content / Article Extractor, web scrapping lib in Python
Pythonic HTML Parsing for Humans™
Quick Overview
Newspaper is a Python library for extracting and curating articles from news websites. It provides tools for web scraping, content extraction, and natural language processing to help developers easily gather and analyze news content programmatically.
Pros
- Easy-to-use API for extracting article content, metadata, and images
- Supports multiple languages and handles various encoding issues
- Includes built-in caching to improve performance and reduce load on target websites
- Offers natural language processing features like keyword extraction and summary generation
Cons
- May struggle with some complex or dynamically generated web pages
- Occasional inaccuracies in content extraction, especially for non-standard article layouts
- Limited customization options for fine-tuning the extraction process
- Dependency on external libraries may lead to compatibility issues in some environments
Code Examples
- Extracting an article:
from newspaper import Article
url = 'http://example.com/article'
article = Article(url)
article.download()
article.parse()
print(article.title)
print(article.text)
- Extracting multiple articles from a news source:
from newspaper import Source
cnn_paper = Source('http://cnn.com')
cnn_paper.build()
for article in cnn_paper.articles[:5]:
article.download()
article.parse()
print(article.title)
- Using natural language processing features:
from newspaper import Article
article = Article('http://example.com/article')
article.download()
article.parse()
article.nlp()
print(article.summary)
print(article.keywords)
Getting Started
To get started with Newspaper, follow these steps:
-
Install the library using pip:
pip install newspaper3k
-
Import the necessary modules in your Python script:
from newspaper import Article, Source
-
Use the
Article
class to extract content from a single article:article = Article('http://example.com/article') article.download() article.parse() print(article.title) print(article.text)
-
For more advanced features, refer to the library's documentation and examples.
Competitor Comparisons
news-please - an integrated web crawler and information extractor for news that just works
Pros of news-please
- More comprehensive extraction of metadata (e.g., authors, publishing date)
- Built-in support for crawling and scraping multiple news websites
- Includes a command-line interface for easy usage
Cons of news-please
- Slower processing speed compared to Newspaper
- Less extensive language support
- More complex setup and configuration
Code Comparison
news-please:
from newsplease import NewsPlease
article = NewsPlease.from_url('https://www.example.com/article')
print(article.title, article.authors, article.date_publish)
Newspaper:
from newspaper import Article
article = Article('https://www.example.com/article')
article.download()
article.parse()
print(article.title, article.authors, article.publish_date)
Both libraries offer similar functionality for extracting article content and metadata. However, news-please provides more detailed metadata extraction out of the box, while Newspaper focuses on simplicity and speed. news-please is better suited for large-scale news crawling projects, while Newspaper is ideal for quick and straightforward article parsing tasks.
fast python port of arc90's readability tool, updated to match latest readability.js!
Pros of python-readability
- Focuses specifically on content extraction and readability
- Lightweight and easy to integrate into existing projects
- Supports multiple languages out of the box
Cons of python-readability
- Less comprehensive feature set compared to newspaper
- May require additional libraries for full article parsing
- Less active development and community support
Code Comparison
python-readability:
from readability import Document
import requests
response = requests.get('http://example.com')
doc = Document(response.text)
print(doc.summary())
print(doc.title())
newspaper:
from newspaper import Article
article = Article('http://example.com')
article.download()
article.parse()
print(article.text)
print(article.title)
Both libraries offer simple ways to extract content from web pages, but newspaper provides a more comprehensive set of features for article parsing and natural language processing. python-readability focuses primarily on content extraction and readability improvement, while newspaper offers additional functionality like keyword extraction, summarization, and language detection.
Html Content / Article Extractor, web scrapping lib in Python
Pros of python-goose
- More mature project with longer development history
- Supports multiple languages out of the box
- Offers more granular control over extraction parameters
Cons of python-goose
- Less actively maintained, with fewer recent updates
- Smaller community and fewer contributors
- More complex setup and usage compared to newspaper
Code Comparison
newspaper:
from newspaper import Article
url = 'http://example.com/article'
article = Article(url)
article.download()
article.parse()
print(article.title)
print(article.text)
python-goose:
from goose3 import Goose
url = 'http://example.com/article'
g = Goose()
article = g.extract(url=url)
print(article.title)
print(article.cleaned_text)
Both libraries offer similar functionality for extracting article content, but newspaper provides a more streamlined API. python-goose requires initializing a Goose object before extraction, while newspaper allows direct creation of Article objects. The method names differ slightly, with newspaper using parse()
and python-goose using extract()
. Overall, newspaper's API is more intuitive and easier to use for basic article extraction tasks.
Pythonic HTML Parsing for Humans™
Pros of requests-html
- Built on top of Requests, providing a familiar API for Python developers
- Supports JavaScript rendering with Pyppeteer, allowing for dynamic content scraping
- Offers a more lightweight and focused approach to web scraping
Cons of requests-html
- Less specialized for news article extraction compared to Newspaper
- Lacks built-in natural language processing features
- May require more manual configuration for specific use cases
Code Comparison
requests-html:
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://python.org/')
r.html.render()
print(r.html.find('title', first=True).text)
Newspaper:
from newspaper import Article
article = Article('http://cnn.com/2014/01/01/world/asia/japan-whale-hunt/index.html')
article.download()
article.parse()
print(article.title)
Both libraries offer straightforward ways to extract content from web pages, but Newspaper is more specialized for article extraction, while requests-html provides a more general-purpose web scraping toolkit.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Newspaper3k: Article scraping & curation
.. image:: https://badge.fury.io/py/newspaper3k.svg :target: http://badge.fury.io/py/newspaper3k.svg :alt: Latest version
.. image:: https://travis-ci.org/codelucas/newspaper.svg :target: http://travis-ci.org/codelucas/newspaper/ :alt: Build status
.. image:: https://coveralls.io/repos/github/codelucas/newspaper/badge.svg?branch=master :target: https://coveralls.io/github/codelucas/newspaper :alt: Coverage status
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`_
.. _tweeted by
: https://twitter.com/kennethreitz/status/419520678862548992
.. _The Changelog
: http://thechangelog.com/newspaper-delivers-instapaper-style-article-extraction/
Newspaper is a Python3 library! Or, view our deprecated and buggy Python2 branch
_
.. _Python2 branch
: https://github.com/codelucas/newspaper/tree/python-2-head
A Glance:
.. code-block:: pycon
>>> from newspaper import Article
>>> url = 'http://fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/'
>>> article = Article(url)
.. code-block:: pycon
>>> article.download()
>>> article.html
'<!DOCTYPE HTML><html itemscope itemtype="http://...'
.. code-block:: pycon
>>> article.parse()
>>> article.authors
['Leigh Ann Caldwell', 'John Honway']
>>> article.publish_date
datetime.datetime(2013, 12, 30, 0, 0)
>>> article.text
'Washington (CNN) -- Not everyone subscribes to a New Year's resolution...'
>>> article.top_image
'http://someCDN.com/blah/blah/blah/file.png'
>>> article.movies
['http://youtube.com/path/to/link.com', ...]
.. code-block:: pycon
>>> article.nlp()
>>> article.keywords
['New Years', 'resolution', ...]
>>> article.summary
'The study shows that 93% of people ...'
.. code-block:: pycon
>>> import newspaper
>>> cnn_paper = newspaper.build('http://cnn.com')
>>> for article in cnn_paper.articles:
>>> print(article.url)
http://www.cnn.com/2013/11/27/justice/tucson-arizona-captive-girls/
http://www.cnn.com/2013/12/11/us/texas-teen-dwi-wreck/index.html
...
>>> for category in cnn_paper.category_urls():
>>> print(category)
http://lifestyle.cnn.com
http://cnn.com/world
http://tech.cnn.com
...
>>> cnn_article = cnn_paper.articles[0]
>>> cnn_article.download()
>>> cnn_article.parse()
>>> cnn_article.nlp()
...
.. code-block:: pycon
>>> from newspaper import fulltext
>>> html = requests.get(...).text
>>> text = fulltext(html)
Newspaper can extract and detect languages seamlessly. If no language is specified, Newspaper will attempt to auto detect a language.
.. code-block:: pycon
>>> 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 :)
.. code-block:: pycon
>>> import newspaper
>>> sina_paper = newspaper.build('http://www.sina.com.cn/', language='zh')
>>> for category in sina_paper.category_urls():
>>> print(category)
http://health.sina.com.cn
http://eladies.sina.com.cn
http://english.sina.com
...
>>> article = sina_paper.articles[0]
>>> article.download()
>>> article.parse()
>>> print(article.text)
æ°æµªæ¦æ±æ±½è½¦ç»¼å éç汽车å¸åºçæ¥è¶æçï¼
ä¼ ç»çâéå
¨å®¶ä¹åæ±å¾ç±è½¦å½âçå
¨é¢è´è½¦æ¨¡å¼å·²ç¶è¿æ¶ï¼
å¦ä¸ç§è½»æ¾çæ°å
´ 车模å¼ââéèè´è½¦æ£éæ¥æ为æ¶ä¸æ¶è´¹è
è´
ä¹°ç±è½¦æ为æ¶å°çæ¶è´¹ç念ï¼ä»ä»¬è®¤ä¸ºï¼è¿ç§æ°é¢çè´è½¦
模å¼æ¢è½å¨çæå
...
>>> print(article.title)
两年åå
0æç»0å©ç ç§é²å
¹æèéèè½»æ¾è´_æ¦æ±è½¦å¸_æ¦æ±æ±½
车ç½_æ°æµªæ±½è½¦_æ°æµªç½
Support our library
It takes only one click
_
Docs
Check out The Docs
_ for full and detailed guides using newspaper.
Interested in adding a new language for us? Refer to: Docs - Adding new languages <https://newspaper.readthedocs.io/en/latest/user_guide/advanced.html#adding-new-languages>
_
Features
- 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
- Works in 10+ languages (English, Chinese, German, Arabic, ...)
.. code-block:: pycon
>>> import newspaper
>>> newspaper.languages()
Your available languages are:
input code full name
ar Arabic
be Belarusian
bg Bulgarian
da Danish
de German
el Greek
en English
es Spanish
et Estonian
fa Persian
fi Finnish
fr French
he Hebrew
hi Hindi
hr Croatian
hu Hungarian
id Indonesian
it Italian
ja Japanese
ko Korean
lt Lithuanian
mk Macedonian
nb Norwegian (Bokmål)
nl Dutch
no Norwegian
pl Polish
pt Portuguese
ro Romanian
ru Russian
sl Slovenian
sr Serbian
sv Swedish
sw Swahili
th Thai
tr Turkish
uk Ukrainian
vi Vietnamese
zh Chinese
Get it now
Run â
pip3 install newspaper3k
â
NOT â pip3 install newspaper
â
On python3 you must install newspaper3k
, not newspaper
. newspaper
is our python2 library.
Although installing newspaper is simple with pip <http://www.pip-installer.org/>
_, you will
run into fixable issues if you are trying to install on ubuntu.
If you are on Debian / Ubuntu, install using the following:
-
Install
pip3
command needed to installnewspaper3k
package::$ sudo apt-get install python3-pip
-
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
NOTE: If you find problem installing libpng12-dev
, try installing libpng-dev
.
-
Download NLP related corpora::
$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python3
-
Install the distribution via pip::
$ pip3 install newspaper3k
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
$ pip3 install newspaper3k
$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python3
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
::
$ pip3 install newspaper3k
$ curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python3
Donations
Your donations are greatly appreciated! They will free me up to work on this project more, to take on things like: adding new features, bug-fix support, addressing concerns with the library.
- My PayPal link:
https://www.paypal.me/codelucas
_ - My
Venmo
_ handle: @Lucas-Ou-Yang
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::
$ pip3 install -r requirements.txt
$ python3 setup.py install
Feel free to give our testing suite a shot, everything is mocked!::
$ python3 tests/unit_tests.py
Planning on tweaking our full-text algorithm? Add the fulltext
parameter::
$ python3 tests/unit_tests.py fulltext
Demo
View a working online demo here: http://newspaper-demo.herokuapp.com
This is another working online demo: http://newspaper.chinazt.cc/
LICENSE
Authored and maintained by Lucas Ou-Yang
_.
Parse.ly
_ sponsored some work on newspaper, specifically focused on
automatic extraction.
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!
.. _Lucas Ou-Yang
: http://codelucas.com
.. _email & contact me
: mailto:lucasyangpersonal@gmail.com
.. _python-goose's
: https://github.com/grangier/python-goose
.. _here
: https://github.com/codelucas/newspaper/blob/master/GOOSE-LICENSE.txt
.. _https://www.paypal.me/codelucas
: https://www.paypal.me/codelucas
.. _Venmo
: https://www.venmo.com/Lucas-Ou-Yang
.. _Quickstart guide
: https://newspaper.readthedocs.io/en/latest/
.. _The Docs
: https://newspaper.readthedocs.io
.. _lxml
: http://lxml.de/
.. _requests
: https://github.com/kennethreitz/requests
.. _Parse.ly
: http://parse.ly
.. _It takes only one click
: https://tracking.gitads.io/?campaign=gitads&repo=newspaper&redirect=gitads.io
Top Related Projects
news-please - an integrated web crawler and information extractor for news that just works
fast python port of arc90's readability tool, updated to match latest readability.js!
Html Content / Article Extractor, web scrapping lib in Python
Pythonic HTML Parsing for Humans™
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot