【问题标题】:How to pass Dynamic values to Url by Loop using Python如何使用 Python 通过循环将动态值传递给 Url
【发布时间】:2020-01-08 04:19:20
【问题描述】:

下面是我用来从存储库中获取值的代码。在这里,我只给出了第 1 页的数据。但是如何使用循环从该存储库中存在的所有页面获取数据。并且这些数据将再次使用 Post 方法发布回另一个 URL。

import requests
import json

headers = {
    'Content-Type': 'application/json, Accept: application/json',
 }

params = (
    ('dql', 'select u.user_name as USER_NAME,u.user_login_name as USER_LOGIN,u.default_folder as 
DEFAULT_FOLDER_PATH, u.last_login_utc_time as USER_LAST_LOGIN, u.user_os_domain as 
USER_OS_DOMAIN,u.user_login_domain as USER_LOGIN_DOMAIN,u.user_state as USER_STATE,f.object_name as 
DEFAULT_FOLDER_NAME,f.r_creation_date as DEFAULT_FOLDER_CREATION_DATE,f.r_modify_date as 
DEFAULT_FOLDER_MODIFY_DATE from dm_user u, dm_folder f where u.default_folder = f.r_folder_path 
enable(ROW_BASED)'),
    ('items-per-page', '2'),
    ('page', '1'),
)
def requestcall():
  myResponse = requests.get('https://1.1.1.1/rest/repositories/hhdht@@', headers=headers, 
params=params, verify=False, auth=('@@@@@@', '#######'))
  print(myResponse.status_code)
    # For successful API call, response code will be 200 (OK)
  if myResponse.ok:

        # extracting data in json format
   data = myResponse.json()


  return data
requestcall()

【问题讨论】:

    标签: python url


    【解决方案1】:

    那么,您可以将page 变量放在for 循环中,直到它达到max_page 这样的数字?

    def requestcall():
      for p in range(max_page):
         params=(
          ('dsql','...')
          ('page',str(p))
          ('item-per-page','2')
         )
         request.get(...params=params)
    

    【讨论】:

    • 这里我不知道要在该范围内提供的存储库的最后页码。对于每个存储库,页面数可能会发生变化。你有什么想法吗..我怎样才能在存储库的最后一页获取数据
    • @saffron 一个api通常可以从一个查询中返回总页数或总记录(然后你可以用它来计算总页数)
    • 我们没有其他选择吗,比如使用指针或其他东西
    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    相关资源
    最近更新 更多