如何使用for请求数组以http获取参数?

2024-04-20 02:45:28 发布

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

以下是我的代码以供您查看:

import warnings
import contextlib
import json
import requests
from urllib3.exceptions import InsecureRequestWarning


old_merge_environment_settings = requests.Session.merge_environment_settings

@contextlib.contextmanager
def no_ssl_verification():
    opened_adapters = set()

    def merge_environment_settings(self, url, proxies, stream, verify, cert):
        # Verification happens only once per connection so we need to close
        # all the opened adapters once we're done. Otherwise, the effects of
        # verify=False persist beyond the end of this context manager.
        opened_adapters.add(self.get_adapter(url))

        settings = old_merge_environment_settings(self, url, proxies, stream, verify, cert)
        settings['verify'] = False

        return settings

    requests.Session.merge_environment_settings = merge_environment_settings

    try:
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', InsecureRequestWarning)
            yield
    finally:
        requests.Session.merge_environment_settings = old_merge_environment_settings

        for adapter in opened_adapters:
            try:
                adapter.close()
            except:
                pass

with no_ssl_verification():
    ##350014,166545
    payload = {'key1': '350014', 'key2': '166545'}
   resp = requests.get('https://rhconnect.marcopolo.com.br/api/workers/data_employee/company/1/badge/params', params=payload, verify=False, headers={'Authorization': 'Token +++++private++++', 'content-type': 'application/json'})
print(resp.status_code)
    print(resp.status_code)
    j = resp.json()
    ##print(j)
    jprint(resp.json())

我如何花一段时间或一段时间向witch one发送个人id号列表并获得JSON结果? 我尝试粘贴一些参数,但不起作用,产生一些错误

我得到以下错误:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

如果我说:

 resp = requests.get('https://rhconnect.marcopolo.com.br/api/workers/data_employee/company/1/badge/350014',

只有一个数字,它就可以工作。 以下是json:

200
[
    {
        "DT_INI_VIG_invalidez": null,
        "DT_fim_VIG_invalidez": null,
        "MODULO": "APOIO",
        "chapa": 350014,
    }
]

Tags: importselfjsonurlsettingsenvironmentsessionmerge
1条回答
网友
1楼 · 发布于 2024-04-20 02:45:28

您必须手动将数字添加到url

"https://rhconnect.marcopolo.com.br/api/workers/data_employee/company/1/badge/" + str(params) 

"https://rhconnect.marcopolo.com.br/api/workers/data_employee/company/1/badge/{}".format(params) 

或者在Python 3.6+中使用f字符串

f"https://rhconnect.marcopolo.com.br/api/workers/data_employee/company/1/badge/{params}"

使用params=params不会以这种方式向url添加数字,而是?key1=350014&key2=166545

您可以使用查看请求使用的url

    print(resp.request.url)

现在您可以在循环中运行

all_results = []

for number in [350014, 166545]:
    url = 'https://rhconnect.marcopolo.com.br/api/workers/data_employee/company/1/badge/{}'.format(number)
    resp = requests.get(url, verify=False, headers={'Authorization': 'Token +++++private++++', 'content-type': 'application/json'})

    #print(resp.request.url)
    print(resp.status_code)
    print(resp.json())

    # keep result on list
    all_results.append(resp.json())

顺便说一句:如果您得到错误,那么您应该检查您得到了什么

print(resp.text)

也许你会得到带有信息或警告的HTML

相关问题 更多 >