首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python 'requests‘包按日期排序

使用python 'requests‘包按日期排序
EN

Stack Overflow用户
提问于 2020-03-06 19:06:46
回答 1查看 1.4K关注 0票数 0

我正在尝试遵循justice.gov API文档页这里,但是我不熟悉这个符号在使用package“这里”时是如何工作的。我想阅读最近的新闻稿,并试图按日期分类。

我使用请求的方式如下。

代码语言:javascript
复制
url = 'http://www.justice.gov/api/v1/press_releases.json?pagesize=10'
fraud = requests.get(url, proxies = proxyDict )

如何按日期进行排序,或从特定日期调用?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-06 19:12:21

它们记录的所有参数都是URL查询参数。只需通过requests参数将它们传递给params,请参阅https://requests.readthedocs.io/en/latest/user/quickstart/#passing-parameters-in-urls

代码语言:javascript
复制
params = {
    "pagesize": 10,
    "sort": "date",
    "direction": "DESC",
}
url = "https://www.justice.gov/api/v1/press_releases.json"
response = requests.get(url, params=params, proxies=proxyDict)
# either check `response.ok` is true or use `response.raise_for_status()
results = response.json()

请注意,我没有在URL中使用?...部分,这让requests来操心。

API提到了按参数值进行筛选的概念;将这些内容放在params字典中,加上parameters[...]文本和方括号;例如,要为number字段的特定值筛选数据,请使用:

代码语言:javascript
复制
params = {
    "parameters[number]" = "09-007",
    ...
}

或者,分配给现有的字典:

代码语言:javascript
复制
params["parameters[number]"] = "09-007"

对于componentstopics列表,API似乎期望关联值的UUID,而不是标题。

演示:

代码语言:javascript
复制
>>> import requests
>>> from pprint import pprint
>>> params = {
...     "pagesize": 10,
...     "sort": "date",
...     "direction": "DESC",
... }
>>> url = "https://www.justice.gov/api/v1/press_releases.json"
>>> response = requests.get(url, params=params)
>>> response = results.json()
>>> pprint(results['metadata'])
{'executionTime': 3.1876049041748047,
 'responseInfo': {'developerMessage': 'OK', 'status': 200},
 'resultset': {'count': 144582, 'page': 0, 'pagesize': 10}}
>>> len(results['results'])
10
>>> pprint(results['results'][0])
{'attachment': [],
 'body': '<p>Indianapolis – United States Attorney Josh J. Minkler announced '
         'today that his office has launched a review of all polling places in '
         'the Southern District of Indiana to determine if they are in '
         'compliance with the Americans with Disabilities Act (ADA) of 1990. '
         'The initiative is in accordance with the federal government’s '
         'congressionally-mandated responsibility to review compliance with '
         'the ADA. It is not in response to any specific complaint against a '
         'county or individual polling location.</p>\n'
         '\n'
         '<p>“This year marks the 30th anniversary of the Americans with '
         'Disabilities Act. Indiana counties have had more than enough time to '
         'ensure that their polling places provide full access to individuals '
         'with disabilities,” said Minkler “Hoosiers in the Southern District '
         'of Indiana, that have a disability, deserve equal access to polling '
         'places and we are committed to making sure that they have it in time '
         'for the 2020 election.”</p>\n'
         '\n'
         '<p>As part of the review, election officials in Indiana’s southern '
         'sixty counties are being asked to complete survey questions '
         'pertaining to polling place accessibility in their county. '
         'Investigators may then conduct on-site inspections to confirm survey '
         'responses and to evaluate compliance with federal ADA regulations. '
         'Counties found to be non-compliant will have the option of resolving '
         'issues informally, and if that effort fails, entering into a '
         'Voluntary Compliance Agreement with the government, whereby they '
         'voluntarily agree to upgrade their facilities, and address issues in '
         'order to meet ADA requirements before the November 2020 '
         'election.</p>\n'
         '\n'
         '<p>Counties found to be engaging in a pattern or practice of '
         'discrimination, or that fail to enter into Voluntary Compliance '
         'Agreements, may face a civil lawsuit brought by the government '
         'and/or be subject to penalties, including monetary penalties and '
         'civil fines.</p>\n'
         '\n'
         '<p>The ADA prohibits discrimination on the basis of disability in '
         'all programs, activities, and services provided by public entities. '
         'The ADA requires that public entities provide voting facilities that '
         'are accessible to people with disabilities.</p>\n'
         '\n'
         '<p>Any citizen with polling place concerns in the Southern District '
         'of Indiana is encouraged to contact Assistant United States Attorney '
         'Jeffrey D. Preston, Civil Rights Coordinator, at 317-226-6333.</p>\n'
         '\n'
         '<p>In October 2017, United States Attorney Josh J. Minkler announced '
         'a Strategic Plan designed to shape and strengthen the District’s '
         'response to its most significant challenges. This initiative '
         'demonstrates the office’s firm commitment to maintaining a robust '
         'program of promoting and enforcing federal civil rights laws. '
         '<i>See</i> United States Attorney’s Office, Southern District of '
         'Indiana <a href="/usao-sdin/StrategicPlan"><u>Strategic '
         'Plan</u></a>\xa0Section 7.3 and 7.4.</p>\n',
 'changed': '1583499835',
 'component': [{'name': 'Civil Rights - Voting Section',
                'uuid': 'cbd106d8-8b13-4d1a-9cd6-3fe4f57e39cf'},
               {'name': 'USAO - Indiana, Southern',
                'uuid': 'f5b4046c-2959-4854-807d-abfc65233e4f'}],
 'created': '1583499810',
 'date': '1583470800',
 'image': [],
 'number': None,
 'teaser': None,
 'title': 'United States Attorney’s Office launches review of Indiana polling '
          'places for compliance with the Americans with Disabilities Act',
 'topic': [],
 'url': 'https://www.justice.gov/usao-sdin/pr/united-states-attorney-s-office-launches-review-indiana-polling-places-compliance',
 'uuid': 'f3cacf9f-e7da-4b18-aaa1-645ccec191af',
 'vuuid': 'd3b2876d-d3ee-4075-8c19-556dee053e46'}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60570195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档