Skip to main content

pynytimes

Run full tests PyPI - Python Version PyPI Downloads DOI

Use all (actually most) New York Times APIs, get all the data you need from the Times!

Documentation

Extensive documentation is available at: https://pynytimes.michadenheijer.com/.

Installation

There are multiple options to install and ugprade pynytimes, but the easiest is by just installing it using pip (or pip3).

Linux and Mac

pip install --upgrade pynytimes

Windows

python -m pip install --upgrade pynytimes

Usage

You can easily import this library using:

from pynytimes import NYTAPI

Then you can simply add your API key (get your API key from The New York Times Dev Portal):

nyt = NYTAPI("Your API key", parse_dates=True)

Make sure that if you commit your code to GitHub you don't accidentially commit your API key.

Supported APIs

When you have imported this library you can use the following features from the New York Times API.

Search

Popular

Metadata

Other

Top stories

To get the current top stories use:

top_stories = nyt.top_stories()

Read the documentation to find the top stories per section.

Most viewed articles

You can also get todays most viewed articles:

most_viewed = nyt.most_viewed()

Read the documentation to get the most viewed articles per week or month.

Most shared articles

To get the most shared articles (shared over email) use:

most_shared = nyt.most_shared()

Read the documentation to get the most shared articles using facebook.

Article search

Search articles using a query using:

articles = nyt.article_search(query="Obama")

In this example we have just defined the content of the search query (Obama), but we can add many more search parameters. Read the documentation to see how.

Book reviews

You can easily find book reviews for every book you've read. You can find those reviews by searching for the author, ISBN or title of the book.

# Get reviews by author (first and last name)
reviews = nyt.book_reviews(author="George Orwell")

# Get reviews by ISBN
reviews = nyt.book_reviews(isbn="9780062963673")

# Get book reviews by title
reviews = nyt.book_reviews(title="Becoming")

Read the documentation to find more information about additional parameters.

Movie reviews

You can not only get the book reviews, but the movie reviews too.

reviews = nyt.movie_reviews(keyword="Green Book")

Read the documentation to find more information about additional parameters.

Best sellers lists

The New York Times has multiple best sellers lists. To get from the fiction best seller list:

# Get fiction best sellers list
books = nyt.best_sellers_list()

Read how to get the other best seller lists in the documentation.

Article metadata

With an URL from a New York Times article you can easily get all the metadata you need from it.

metadata = nyt.article_metadata(
    url = "https://www.nytimes.com/2019/10/20/world/middleeast/erdogan-turkey-nuclear-weapons-trump.html"
)

Read additional parameters in the documentation.

Load latest articles

You can easily load the latest articles published by the New York Times.

latest = nyt.latest_articles(
    source = "nyt",
    section = "books"
)

Additional parameters can be found in the documentation.

Tag query

The New York Times has their own tags library. You can query this library with this API.

tags = nyt.tag_query(
    "pentagon",
    max_results = 20
)

Additional parameters can be found in the documentation.

Archive metadata

If you want to load all the metadata from a specific month, then this API makes that possible. Be aware you'll download a big JSON file (about 20 Mb), so it can take a while.

import datetime

data = nyt.archive_metadata(
    date = datetime.datetime(2019, 1, 1)
)

Read more in the documentation.

Citing this Repository

If you use pynytimes, a citation would be very much appriciated. If you're using BibTeX you can use the following citation:

@software{Den_Heijer_pynytimes_2023,
    author = {Den Heijer, Micha},
    license = {MIT},
    title = {{pynytimes}},
    url = {https://github.com/michadenheijer/pynytimes},
    version = {0.10.0},
    year = {2023},
    doi = {10.5281/zenodo.7821090}
}

If you're not using BibTeX, you can retrieve the preferred citation from Zenodo.

License

License: MIT

Disclaimer: This project is not made by anyone from the New York Times, nor is it affiliated with The New York Times Company.

Metadata

Release files for pynytimes 0.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pynytimes 0.10.0
File Size Uploaded Provenance
pynytimes-0.10.0.tar.gz 20.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pynytimes 0.10.0
File Interpreter ABI Platform
pynytimes-0.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 40.9 kB

Release files / pynytimes-0.10.0.tar.gz

Download URL pynytimes-0.10.0.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cafc3f19e693ca7fb387820c19e32f4eee1b31ebdb89d599a57267d5dbd7e68f
BLAKE2b-256 checksum
How to use checksums
58c30966c72a520a2d317739f04129503c3f97c6a5e3678ccc07962127be4cea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / pynytimes-0.10.0-py3-none-any.whl

Download URL pynytimes-0.10.0-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ea814927ed37b0b8438d25764636ba297d8321a7282cf52c92abcad259b78cd
BLAKE2b-256 checksum
How to use checksums
959c6dac949d6a957a2046478c759301e09eb47c305bc592c70a5a7772d1e6f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Security for pynytimes 0.10.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page