How andythefactory/newspaper4k works
Project type and purpose
• Newspaper4k is a Python library and CLI tool for scraping, parsing, and curating news articles from arbitrary websites and Google News.
• It provides a Command-Line Interface (CLI) entrypoint and a Python API (Article, Source, GoogleNewsSource classes).
• Core features: multilingual support, multi-threaded download, extractors for body/metadata/images/videos, NLP summarization/keywords, output formatters (JSON/CSV/Text).
File structure
• Top-level package “newspaper/” contains the core modules:
– __main__.py & cli.py: CLI entrypoint and argument parsing
– api.py, article.py, source.py, google_news.py: main API classes
– configuration.py: global settings (threads, language, timeouts)
– network.py: HTTP fetching
– cleaners.py & parsers.py: HTML cleanup and parsing
– extractors/: modular extractors for title, body, authors, images, metadata, video, categories
– nlp.py: keyword extraction and summarization
– outputformatters.py: formatting pipeline results
– languages/, resources/: language-specific stopwords, source lists, user-agents
– mthreading.py: threading helper for concurrent downloads
– utils/: support classes & helpers
• Project root contains CI configs (.github/, .travis.yml, ReadTheDocs), docs/, tests/, setup.py/pyproject.toml, requirements, license.
README
• Confirms the layering: CLI → API → build/download → parse/extract → NLP → output.
• Notes Google News integration via GNewsSource (requires external Gnews package).
• Highlights multi-language, multi-thread, plugin-style extractors.
Main components & interactions
a. User Interface layer
– User/Developer invokes either:
• CLI (python -m newspaper …) → newspaper/__main__.py → cli.py
• Python API (import newspaper; newspaper.article() or newspaper.build())
b. Configuration
– Read from Configuration class or CLI flags (language, threads, output format)
c. Download & Networking
– Source.download_articles() or Article.download() uses network.py (+ mthreading.py)
– Fetch HTML via requests/urllib (and optionally cloudscraper for JS-heavy sites)
d. Parsing & Cleaning
– cleaners.py strips unwanted tags; parsers.py uses lxml/BeautifulSoup to build DOM
e. Extraction pipeline
– extractors/*.py run in sequence on cleaned HTML:
• MetadataExtractor, TitleExtractor, AuthorsExtractor, PubDateExtractor, CategoriesExtractor
• ContentExtractor (article body), ImageExtractor, VideosExtractor
f. NLP & Summarization
– nlp.py tokenizes, extracts keywords, builds summary
– uses language resources (stopword lists in resources/text)
g. Output Formatting
– outputformatters.py serializes results to JSON/CSV/Text
h. External integrations
– GoogleNewsSource (in google_news.py) uses the Gnews package
– feedparser for RSS, PIL/Lxml for image parsing
Architectural patterns & design principles
• Layered architecture: UI → API → Services (download, parse, extract, NLP) → Data/resources → External
• Pipeline design: each step (cleaners → parsers → extractors → NLP → output) is a pluggable stage
• Configuration-driven: behavior (threads, language) is centralized in Configuration
• Concurrency: ThreadPoolExecutor for multi-threaded downloads
• Extensibility: extractors and output formatters can be extended or swapped out