(大多数)纽约时报API的Python包装器
pynytimes的Python项目详细描述
pynytimes公司
使用所有(实际上大部分)纽约时报API,从时报获取您需要的所有数据!在
安装
{praugde>安装是最简单的方法。在
Linux和Mac
pip install --upgrade pynytimes
Windows
^{pr2}$开发
您也可以从GitHub本身手动安装pynytimes
。这可以通过先克隆这个存储库,然后使用Python安装它来完成。这可能安装一个未发布的版本,只有当您想修改代码或帮助维护这个库时,才建议使用此方法安装。
git clone https://github.com/michadenheijer/pynytimes.git
cd pynytimes
python setup.py install
使用
您可以使用以下方法轻松导入此库:
frompynytimesimportNYTAPI
然后您可以简单地添加API密钥(从The New York Times Dev Portal获取API密钥):
nyt=NYTAPI("Your API key")
您还可以选择使用http
而不是{
nyt=NYTAPI("Your API key",https=False)
Variables | Description | Data type | Required |
---|---|---|---|
^{ | The API key from The New York Times | ^{ | True |
^{ | Whether you'd want requests over https | ^{ | False |
^{ | A requests session that you'd like the wrapper to use | ^{ | False |
支持的API
导入此库后,可以使用《纽约时报》API中的以下功能。在
- Top stories
- Most viewed articles
- Most shared articles
- Article search
- Book reviews
- Movie reviews
- Best sellers lists
- Article metadata (Times Wire)
- Load latest articles (Times Wire)
- Tag query (TimesTags)
- Archive metadata
热门故事
你可以向《纽约时报》索取头条新闻。你也可以从一个特定的部分得到最重要的故事。在
top_stories=nyt.top_stories()# Get all the top stories from a specific categorytop_science_stories=nyt.top_stories(section="science")^{tb2}$
可能的部分包括:艺术、汽车、书籍、商业、时尚、食品、健康、家庭、内部人、杂志、电影、国家、纽约地区、讣告、观点、政治、房地产、科学、体育、周日回顾、技术、剧院、tmagazine、旅行、upshot和world。在
浏览次数最多的文章
纽约时报API可以提供最近一天、一周或一个月最受欢迎的文章。在
most_viewed=nyt.most_viewed()# Get most viewed articles of last 7 or 30 daysmost_viewed=nyt.most_viewed(days=7)most_viewed=nyt.most_viewed(days=30)^{3}$
分享最多的文章
您不仅可以从《纽约时报》API请求最多浏览的文章,还可以请求最多共享的文章。你甚至可以请求通过电子邮件和Facebook分享最多的文章。你可以每天、每周或每月获得最多的共享文章。在
most_shared=nyt.most_shared()# Get most emaild articles of the last daymost_shared=myt.most_shared(days=1,method="email")# Get most shared articles to Facebook of the last 7 daysmost_shared=nyt.most_shared(days=7,method="facebook")# Get most shared articles to Facebook of the last 30 daysmost_shared=nyt.most_shared(days=30,method="facebook")
Variables | Description | Data type | Required |
---|---|---|---|
^{ | Get most viewed articles over the last ^{ | ^{ | False |
^{ | Method of sharing (^{ | ^{ | False |
文章搜索(测试版)
你也可以搜索纽约时报的所有文章。您可以选择定义搜索查询(使用query
选项)、结果数量(使用results
)和所需的结果数量。您甚至可以添加更多选项,以便过滤结果。在
importdatetimearticles=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 |
---|---|---|---|
^{ | What you want to search for | ^{ | False |
^{ | The amount of results that you want to receive (returns a multiple of 10) | ^{ | False |
^{ | A dictionary of the dates you'd like the results to be between | ^{ | False |
^{ | A dictionary of additional options | ^{ | False |
dates
Variables | Description | Data type | Required |
---|---|---|---|
^{ | Results should be published at or after this date | ^{ | False |
^{ | Results should be published at or before this date | ^{ | False |
options
Variables | Description | Data type | Required |
---|---|---|---|
^{ | How you want the results to be sorted (^{ | ^{ | False |
^{ | Results should be from one of these sources | ^{ | False |
^{ | Results should be from one of these news desks (valid options) | ^{ | False |
^{ | Results should be from this type of material (valid options) | ^{ | False |
^{ | Results should be from this section (valid options) | ^{ | False |
书评
你可以很容易地找到你读过的每本书的书评。你可以通过搜索作者、ISBN或书名来找到这些评论。在
# Get reviews by author (first and last name)reviews=nyt.book_reviews(author="George Orwell")# Get reviews by ISBNreviews=nyt.book_reviews(isbn=9780062963673)# Get book reviews by titlereviews=nyt.book_reviews(title="Becoming")
Variables | Description | Data type | Required |
---|---|---|---|
^{ | Reviews of books from this author | ^{ | One of these three |
^{ | Reviews of books with this ISBN | ^{ | One of these three |
^{ | Reviews of books with this title | ^{ | One of these three |
影评
你不仅可以得到书评,还有电影评论。在
importdatetimereviews=nyt.movie_reviews(keyword="Green Book",options={"order":"by-opening-date","reviewer":"A.O. Scott","critics_pick":False},dates={"opening_date_start":datetime.datetime(2017,1,1),"opening_date_end":datetime.datetime(2019,1,1),"publication_date_start":datetime.datetime(2017,1,1),"publication_date_end":datetime.datetime(2019,1,1)})
Variables | Description | Data type | Required |
---|---|---|---|
^{ | Reviews of movies with this keyword | ^{ | False |
^{ | Dictionary of search options | ^{ | False |
^{ | Dictionary of dates about the review | ^{ | False |
options
Variables | Description | Data type | Required |
---|---|---|---|
^{ | How to sort the results (^{ | ^{ | False |
^{ | Name of the reviewer | ^{ | False |
^{ | Only return critics' pick if ^{ | ^{ | False |
dates
Variables | Description | Data type | Required |
---|---|---|---|
^{ | Reviews about movies released at or after this date | ^{ | False |
^{ | Reviews about movies released at or before this date | ^{ | False |
^{ | Reviews released at or after this date | ^{ | False |
^{ | Reviews released at or before this date | ^{ | False |
畅销书排行榜
《纽约时报》有多个畅销书排行榜。您可以使用此库轻松请求这些列表。在
# Get all the available New York Times best sellers listslists=nyt.best_sellers_lists()# Get fiction best sellers listbooks=nyt.best_sellers_list()# Get non-fiction best sellers listbooks=nyt.best_sellers_list(name="combined-print-and-e-book-nonfiction")# Get best sellers lists from other dateimportdatetimebooks=nyt.best_sellers_list(name="combined-print-and-e-book-nonfiction",date=datetime.datetime(2019,1,1))
Variables | Description | Data type | Required |
---|---|---|---|
^{ | Name of best sellers list | ^{ | False |
^{ | Date of best sellers list | ^{ | False |
文章元数据
有了一个来自纽约时报文章的URL,你可以很容易地从中获得所需的所有元数据。在
metadata=nyt.article_metadata(url="https://www.nytimes.com/2019/10/20/world/middleeast/erdogan-turkey-nuclear-weapons-trump.html")
Variables | Description | Data type | Required |
---|---|---|---|
^{ | URL of the article | ^{ | True |
加载最新文章
你可以很容易地载入纽约时报发表的最新文章。在
latest=nyt.latest_articles(source="nyt",section="books")
Variables | Description | Data type | Required |
---|---|---|---|
^{ | Source of article (^{ | ^{ | False |
^{ | Section of articles | ^{ | False |
您可以使用以下方法找到所有可能的部分:
sections=nyt.section_list()
标签查询
《纽约时报》有自己的标签库。您可以使用此API查询此库。在
tags=nyt.tag_query("pentagon",max_results=20)^{tb15}$
归档元数据
如果您想从一个特定的月份加载所有元数据,那么这个API使之成为可能。请注意,您将下载一个大的JSON文件(约20mb),因此需要一段时间。在
importdatetimedata=nyt.archive_metadata(date=datetime.datetime(2019,1,1))
Variables | Description | Data type | Required |
---|---|---|---|
^{ | Date of month of all the metadata | ^{ | True |
许可证
免责声明:这个项目不是由纽约德州理工学院的任何人制作的它也不隶属于纽约时报公司。
- 项目
标签: