Python:替换 Python 文本 - CURL Python

Python: Replace Python text - CURL Python

我需要对定义为NUEMRODEDNI的变量进行调和,如何实现?

不好意思我是新手

我不知道如何使用python中定义的变量,我需要如图所示替换它

import string

import requests

from requests.structures import CaseInsensitiveDict



url = "www.url.com:8022/SistemaIdentificacion/servlet/com.personas.consultapersona?030bf8cfcd4bfccbd543df61b1b43f67,gx-no-cache=1648440596691"



NUEMRODEDNI = "41087712"



headers = CaseInsensitiveDict()

headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0"

headers["Accept"] = "*/*"

headers["Accept-Language"] = "es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3"

headers["Accept-Encoding"] = "gzip, deflate"

headers["GxAjaxRequest"] = "1"

headers["Content-Type"] = "application/json"

headers["AJAX_SECURITY_TOKEN"] = "a6da9873adb..."

headers["X-GXAUTH-TOKEN"] = "eyJ0eXAiOiJ..."

headers["Origin"] = "http://www.url.com"

headers["Connection"] = "keep-alive"

headers["Referer"] = "www.url.com/SistemaIdentificacion/servlet/com.personas.consultapersona"

headers["Cookie"] = "GX_CLIENT_ID=0496f100-9e4e-4e36-a68d-ba3770ee2bff; GX_SESSION_ID=KUqyHU%2FZbpu96sYlj7Gry8bCYpV6CaSgVk0BLxVCpAU%3D; JSESSIONID=1812E6AC00940BDB325EF9592CB93FF8; GxTZOffset=America/Argentina/Buenos_Aires"



data = '{"MPage":false,"cmpCtx":"","parms":[EDIT HERE,{"s":"M","v":[["","(None)"],["F","Femenino"],["M","Masculino"]]},"M",{"User":"","CompanyCode":0,"Profile":"","UsrOneLoginID":"6647","Depid":1,"UsrLP":6488,"unidad":"","unidadid":"68","IMFParteCuerpo":"","denunciasid":0,"destino":"68","TipoPersona":"","NombreArchivo":"","denorigen":"","macdestinoscodorganigrama":""}],"hsh":[],"objClass":"consultapersona","pkgName":"com.personas","events":["ENTER"],"grids":{}}'





resp = requests.post(url, headers=headers, data=data)



print(resp.content)

请提供您的代码输入而不是屏幕截图,以便我们可以简单地复制和运行它在我们这边。

不过你应该试试:

NUMERODNI = "41087712"
data = '{"MPage":false,"cmpCtx":"","parms":[EDIT HERE,{"s":"M","v":[["","(None)"],["F","Femenino"],["M","Masculino"]]},"M",{"User":"","CompanyCode":0,"Profile":"","UsrOneLoginID":"6647","Depid":1,"UsrLP":6488,"unidad":"","unidadid":"68","IMFParteCuerpo":"","denunciasid":0,"destino":"68","TipoPersona":"","NombreArchivo":"","denorigen":"","macdestinoscodorganigrama":""}],"hsh":[],"objClass":"consultapersona","pkgName":"com.personas","events":["ENTER"],"grids":{}}'

data = data.replace("EDIT HERE", NUMERODNI)

print(data) # '{... "parms":[41087712, ...}'

解决方案明确地交付 所需的字符串 作为结果。 如果您的代码仍然没有按照您的意愿执行,那么实际问题必须出在其他地方。

由于您要发布 JSON 数据,因此将数据保存为 Python 字典并告诉请求 JSON-ify 它会更容易 (requests.post(json=...))。

这样就不需要字符串替换,只需使用变量即可。

我也借此机会缩短了你的 headers 结构——它可以只是一个命令。

import requests

url = "www.url.com:8022/SistemaIdentificacion/servlet/com.personas.consultapersona?030bf8cfcd4bfccbd543df61b1b43f67,gx-no-cache=1648440596691"

NUEMRODEDNI = "41087712"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0",
    "Accept": "*/*",
    "Accept-Language": "es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3",
    "Accept-Encoding": "gzip, deflate",
    "GxAjaxRequest": "1",
    "Content-Type": "application/json",
    "AJAX_SECURITY_TOKEN": "a6da9873adb...",
    "X-GXAUTH-TOKEN": "eyJ0eXAiOiJ...",
    "Origin": "http://www.url.com",
    "Connection": "keep-alive",
    "Referer": "www.url.com/SistemaIdentificacion/servlet/com.personas.consultapersona",
    "Cookie": "GX_CLIENT_ID=0496f100-9e4e-4e36-a68d-ba3770ee2bff; GX_SESSION_ID=KUqyHU%2FZbpu96sYlj7Gry8bCYpV6CaSgVk0BLxVCpAU%3D; JSESSIONID=1812E6AC00940BDB325EF9592CB93FF8; GxTZOffset=America/Argentina/Buenos_Aires",
}

data = {
    "MPage": False,
    "cmpCtx": "",
    "parms": [
        NUEMRODEDNI,
        {"s": "M", "v": [["", "(None)"], ["F", "Femenino"], ["M", "Masculino"]]},
        "M",
        {
            "User": "",
            "CompanyCode": 0,
            "Profile": "",
            "UsrOneLoginID": "6647",
            "Depid": 1,
            "UsrLP": 6488,
            "unidad": "",
            "unidadid": "68",
            "IMFParteCuerpo": "",
            "denunciasid": 0,
            "destino": "68",
            "TipoPersona": "",
            "NombreArchivo": "",
            "denorigen": "",
            "macdestinoscodorganigrama": "",
        },
    ],
    "hsh": [],
    "objClass": "consultapersona",
    "pkgName": "com.personas",
    "events": ["ENTER"],
    "grids": {},
}

resp = requests.post(url, headers=headers, json=data)
resp.raise_for_status()
print(resp.content)