2dareis2do/newspaper-playwright-wrapper

PHP wrapper for Newspaper3/4k Article and Playwright scraping & curation

2.0.0 2025-03-19 19:15 UTC

This package is auto-updated.

Last update: 2025-03-20 14:50:26 UTC


README

Software License Packagist Version

Simple php wrapper for Newspaper3/4k Article scraping and curation with Playwright support.

The combination of using Newspaper3/4k script with Playwright allows for the automated handling of cookies and other actions required by cookie compliance on a site by site basis.

Now you can also pass a third parameter to the scrape method. This allows you to override the included ArticleScraping.py on a site by site basis.

Update

2.0.0 introduces an additional parameter for a client to pass command parameter. This is useful where multiple versions of python (with respective dependencies) may be available on a single server. If no value is passed, it will default to the use the default python string. This supports both absolute or relative paths.

Install Necessary Packages

pip install newspaper4k
pip install playwright
python -m playwright install
python -m playwright install-deps

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 install newspaper3k 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.

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

Install on venv

The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site directories. A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available. Python

also,

A common directory location for a virtual environment is .venv. This name keeps the directory typically hidden in your shell and thus out of the way while giving it a name that explains why the directory exists. It also prevents clashing with .env environment variable definition files that some tooling supports.

Bearing this in mind here this is the recommended way to install your dependencies:

  1. If installing for the first time you may need to make sure pip is enabled. On ubuntu 22.x first update apt e.g. apt update then install apt install python3-pip

  2. If installing for the first time you may also need to make sure venv is available. On ubuntu 22.x it can be downloaded like so apt install python3-venv

  3. Decide where you want to set up you venv. This can be somehwere on your virtual host. You can use the following syntax: python -m venv /path/to/new/virtual/.venv

  4. Activate your .venv in your current session. e.g. source /path/to/new/virtual/.venv/bin/activate

  5. The first time you set up your script you will likely need to download and install any necessary dependencies. You can use pip to help with this form your Virtual session. Once you have installed your dependencies, you can export a list to use for subsequent installs e.g. python -m pip freeze > /path/to/requirements.txt

  6. Exit your virtual environment. e.g. deactivate

Subsequent installs

The next time you have to set up your dependencies, you can now start using pip to install them automatically. e.g. python -m pip install -r /path/to/requirements.txt

Running on the server

Chances are you web server does not run a virtual environment session. However, we can still specify the path to python in our newly created virtual environment folder and python will automatically load your installed dependencies (unlike the global server version). e.g. We can pass the absolute path to the version of python we want to use by passing the following $command parameter: /path/to/python/.venv/bin/python

This can also be passed relatively which can be more robust across different environments. e.g. ../relative/path/to/python/.venv/bin/python

If we do not a path for $command, it will default to use the globally installed verion of python.

Installation

composer require 2dareis2do/newspaper-playwright-wrapper

1 time download of the sentence tokenizer

After installing the NLTK package, please do install the necessary datasets/models for specific functions to work.

In particular you will need the Punkt Sentence Tokenizer. https://www.nltk.org/api/nltk.tokenize.punkt.html

e.g.

$ python

loads python interpreter:

>>> import nltk
>>> nltk.download('all')

or

>>> nltk.download('punkt')

Usage

use Twodareis2do\ScrapeNewspaperPlaywright\NewspaperPlaywrightWrapper;

$parser = new NewspaperPlaywrightWrapper();

$parser->scrape('your url', false);

Read more

(Newspaper)[https://github.com/codelucas/newspaper]

(nltk)[http://www.nltk.org/install.html]

(Scrape & Summarize News Articles Using Python)[https://medium.com/@randerson112358/scrape-summarize-news-articles-using-python-51a48af1b4e2]