纽约时报头条故事api的python包装器

nytimes-top-stories的Python项目详细描述


nytimes-top-stories是纽约时报顶部的一个简单的python包装器 故事API。

与Python2.7和3+兼容。

依赖关系

nytimes-top-stories需要requests包。

安装

PIP

pip install nytimes-top-stories

克隆库

  • git clone git@github.com:kashiish/nytimes-top-stories.git
  • cd进入nytimes-top-stories目录
  • python setup.py install

用法

在注册TopStories API密钥 https://developer.nytimes.com/signup

fromtopstoriesimportTopStoriesAPIapi=TopStoriesAPI(<SECRET_API_KEY>)

get_stories方法接受一个必需参数和两个可选参数 论据。签出API documentation以查看 从API返回。

defget_stories(self,section,format_type="json",return_json_string=False):"""
  Gets a list of current top articles and associated images in the
  specified section and in the specified format.

  params:
      section: (string) the section the articles appears in
      format: (string) json or jsonp, default is json
      return_json_string: (boolean) if True, return value will be JSON string instead of a python list,
                          default is False
  return:
      format_type=json: A list of articles (articles are python dicts) is returned.
                        If return_json_string, string is returned.
      format_type=jsonp: API returns a callback function (string) in the format
                        "{section}TopStoriesCallback({data})".
                        {data} is an object, not an array, and it is not parsed/decoded.
  """
stories=api.get_stories("politics")# list of story dictsstories_string=api.get_stories("home",return_json_string=True)# json stringstories_jsonp=api.get_stories("work",format_type="jsonp")# (string) callback function with data input

此外,还有两种公共方法:

#returns a list of valid sections>>>api.get_sections_list()>>>["home","opinion","world","national","politics","upshot","nyregion","business","technology","science","health","sports","arts","books","theater","sundayreview","fashion","tmagazine","food","travel","magazine","realestate","automobiles","obituaries","insider"]
# writes a list of stories to a json file# input a path to file and list of storiesapi.write_to_json_file("example.json",stories)

测试

pytestmock用于测试此包。

python setup.py pytest

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何用java表示这个数学函数的算法   Java/Stream帮助:仅使用streams将嵌套的映射列表转换为映射   使用Selenium连接到数据库时发生java未知主机异常   java如何了解jvm内存使用:“堆内存”和“堆外内存”   java Oracle BI报告导入模板   java如何使用Spring将xml转换为bean?   java线程。join()以保证执行顺序   java从THINGSPEAK到ANDROID应用程序获取JSON数据   使用Java的stanford库中的异常   java正确使用来自其他类文件的方法   如果集合中的元素类型为接口类型,如何填充集合?(爪哇)   记录java。util。记录器创建的文件超过了应有的数量   类Java对象uniq值   尝试调用无法应用于()的方法时出现java错误