# pynytimes
[<img src="https://raw.githubusercontent.com/michadenheijer/pynytimes/main/.github/poweredby_nytimes.png" height="20px">](https://developer.nytimes.com/) [![Build Status](https://travis-ci.com/michadenheijer/pynytimes.svg?token=8nhCHVYqgufX65p8PRDx&branch=main)](https://travis-ci.com/michadenheijer/pynytimes) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pynytimes)](https://pypi.org/project/pynytimes/) [![PyPI](https://img.shields.io/pypi/v/pynytimes)](https://pypi.org/project/pynytimes/)
Use all (actually most) New York Times APIs, get all the data you need from the Times!
## Installation
There are multiple options to install and ugprade pynytimes, but the easiest is by just installing it using ```pip``` (or ```pip3```). *You can also optionally install ```orjson``` for faster json parsing.*
### Linux and Mac
```bash
pip install --upgrade pynytimes
```
### Windows
```shell
python -m pip install --upgrade pynytimes
```
### Development
You can also install ```pynytimes``` manually from GitHub itself. This can be done by cloning this repository first, and then installing it using Python. *This might install an unreleased version, installation using this method is only advised if you want to modify the code or help maintain this library.*
```bash
git clone https://github.com/michadenheijer/pynytimes.git
cd pynytimes
python setup.py install
```
## Usage
You can easily import this library using:
```python
from pynytimes import NYTAPI
```
Then you can simply add your API key (get your API key from [The New York Times Dev Portal](https://developer.nytimes.com/)):
```python
nyt = NYTAPI("Your API key", parse_dates=True)
```
**Optionally you can also set to use ```http``` instead of ```https```.**
```python
nyt = NYTAPI("Your API key", https=False)
```
| Variables | Description | Data type | Required |
|------------------|-----------------------------------------------------------------------|---------------------------------|----------|
| ```key``` | The API key from [The New York Times](https://developer.nytimes.com/) | ```str``` | True |
| ```https``` | Whether you'd want requests over https | ```bool``` | False |
| ```session``` | A requests session that you'd like the wrapper to use | ```requests.sessions.Session``` | False |
| ```backoff``` | Enable [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) | ```bool``` | False |
| ```user_agent``` | The user agent that the client uses | ```str``` | False |
| ```parse_dates```| Optionally disable the automatic parsing of dates (usually this is disabled) | ```bool``` | False |
### Supported APIs
When you have imported this library you can use the following features from the New York Times API.
- [Top stories](#top-stories)
- [Most viewed articles](#most-viewed-articles)
- [Most shared articles](#most-shared-articles)
- [Article search](#article-search)
- [Book reviews](#book-reviews)
- [Movie reviews](#movie-reviews)
- [Best sellers lists](#best-sellers-lists)
- [Article metadata (Times Wire)](#article-metadata)
- [Load latest articles (Times Wire)](#load-latest-articles)
- [Tag query (TimesTags)](#tag-query)
- [Archive metadata](#archive-metadata)
### Top stories
You can request the top stories from the New York Times. You can also get the top stories from a specific section.
```python
top_stories = nyt.top_stories()
# Get all the top stories from a specific category
top_science_stories = nyt.top_stories(section = "science")
```
| Variables | Description | Data type | Required |
|-----------------|-----------------------------------------|-----------------|----------|
| ```section``` | Get Top Stories from a specific section | ```str``` | False |
The possible sections are: arts, automobiles, books, business, fashion, food, health, home, insider, magazine, movies, national, nyregion, obituaries, opinion, politics, realestate, science, sports, sundayreview, technology, theater, tmagazine, travel, upshot, and world.
### Most viewed articles
The New York Times API can provide the most popular articles from the last day, week or month.
```python
most_viewed = nyt.most_viewed()
# Get most viewed articles of last 7 or 30 days
most_viewed = nyt.most_viewed(days = 7)
most_viewed = nyt.most_viewed(days = 30)
```
| Variables | Description | Data type | Required |
|-----------------|--------------------------------------------------------------------------|-----------------|----------|
| ```days``` | Get most viewed articles over the last ```1```, ```7``` or ```30``` days | ```int``` | False |
### Most shared articles
Not only can you request the most viewed articles from the New York Times API, you can also request the most shared articles. You can even request the articles that are most shared by email and Facebook. You can get the most shared articles per day, week or month.
```python
most_shared = nyt.most_shared()
# Get most emaild articles of the last day
most_shared = myt.most_shared(
days = 1,
method = "email"
)
# Get most shared articles to Facebook of the last 7 days
most_shared = nyt.most_shared(
days = 7,
method = "facebook"
)
# Get most shared articles to Facebook of the last 30 days
most_shared = nyt.most_shared(
days = 30,
method = "facebook"
)
```
| Variables | Description | Data type | Required |
|-----------------|--------------------------------------------------------------------------|-----------------|----------|
| ```days``` | Get most viewed articles over the last ```1```, ```7``` or ```30``` days | ```int``` | False |
| ```method``` | Method of sharing (```email``` or ```facebook```) | ```str``` | False |
### Article search
You can also search all New York Times articles. Optionally you can define your search query (using the ```query``` option), the amount of results (using ```results```) and the amount of results you'd like. You can even add more options so you can filter the results.
```python
import datetime
articles = nyt.article_search(
query = "Obama",
results = 30,
dates = {
"begin": datetime.datetime(2019, 1, 31),
"end": datetime.datetime(2019, 2, 28)
},
options = {
"sort": "oldest",
"sources": [
"New York Times",
"AP",
"Reuters",
"International Herald Tribune"
],
"news_desk": [
"Politics"
],
"type_of_material": [
"News Analysis"
]
}
)
```
| Variables | Description | Data type | Required |
|-----------------------------|---------------------------------------------------------------------------------------|-----------------|----------|
| ```query``` | What you want to search for | ```str``` | False |
| ```results``` | The amount of results that you want to receive (returns a multiple of 10) | ```int``` | False |
| [```dates```](#dates) | A dictionary of the dates you'd like the results to be between | ```dict``` | False |
| [```options```](#options) | A dictionary of additional options | ```dict``` | False |
#### ```dates```
| Variables
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共14个文件
py:4个
txt:4个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:pynytimes-0.5.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
pynytimes-0.5.0.tar.gz (14个子文件)
pynytimes-0.5.0
MANIFEST.in 55B
pynytimes.egg-info
PKG-INFO 20KB
requires.txt 17B
SOURCES.txt 288B
top_level.txt 10B
dependency_links.txt 1B
PKG-INFO 20KB
.github
poweredby_nytimes.png 1KB
setup.cfg 38B
pynytimes
__init__.py 262B
api.py 21KB
__version__.py 309B
setup.py 2KB
README.md 16KB
共 14 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功