首页 > 解决方案 > How to pass Dynamic values to Url by Loop using Python

问题描述

Below is the code i am using to get values from the repositories. Here i have given like to give data from only page 1. But how to get data from all the pages existing in that repository using loop. And again these data will be posted back to another URL using Post method.

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()

标签: pythonurl

解决方案


Well, you can put page variable inside the for loop until it reach the max_page number like this?

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

推荐阅读