使用python“requests”包按日期排序

2024-10-06 00:25:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图了解justice.gov API文档页面here,但不熟悉在使用python包“requests”时这种表示法是如何工作的。我想阅读最新的新闻稿,并试图按日期排序

我使用请求的方式如下

url = 'http://www.justice.gov/api/v1/press_releases.json?pagesize=10'
fraud = requests.get(url, proxies = proxyDict )

如何按日期排序,或从特定日期拨打电话


Tags: 文档apihttpurlhere排序www方式
1条回答
网友
1楼 · 发布于 2024-10-06 00:25:36

它们记录的所有参数都是URL查询参数。只需通过params参数将它们传递给requests,请参见Passing Parameters In URLs

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提到了按参数值过滤的概念;用parameters[...]文本和方括号将其放入params字典;e、 g.要在数据上筛选number字段的特定值,请使用:

params = {
    "parameters[number]" = "09-007",
    ...
}

或者,分配给现有词典:

params["parameters[number]"] = "09-007"

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

演示:

>>> 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'}

相关问题 更多 >