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4tYbu2e3njV4nGCrAEflXGsLJU3T5tDzY4GapSo82j8jwjTiJNW0JEOWEkakDsfOY/1Fdv4/Xz/Fnh+3H3mdR+cgH9Kuj4Z2UF3HdWmo3MUsbiRCyKwBByOMCpdV8I6rfeI4NZXUreR7d1MUUkRUBVOQMgmsI0Kkabi1u0c0MLWhSlBx3a7bIzPixIBDpUXctK35bf8AGuH8NLv8T6YP+nmPP/fQruvGHhfxH4g1FZ1jtDDCpSJElOcZzk5A5rAXSb7QPFun32o2n2eB5w5KsGROeeR+dZ14Sdbna0ujLFU5yxPtGmldHsleEeLbnzfF+pyxN0lKZHsNp/ka9b13xHa6VpzPDKk93IMW8MZ3","captchaMime":"image/jpeg","captchaToken":"ALXfmJpxoaxq6LYBXm-kJzIl0Yd5mHG1XbttsBX-EKxMYtYNIc6uTv89fmRxeWZGEgpi2L9sjXYlkm6Vplav_wy2KjdB5J4j3i5fB6CEuPOMXIjEql6mPBJ8-YJTCpOzzk8kOcW5nuBbuLOdMVyVxquLbWjqLZzHeN0iT4Jm4SIZ9mQNfapNGkE","status":"CAPTCHA"}'
love = '"captchaData":"mydata"'
session_token_temp = re.search(r'(\"captchaData\":\")(\w*)', 
response_text).group()
session_token = str(session_token_temp)

Я хочу извлечь значение captchaData и captchaToken и добавить данные в строку, как это

extracted_data = (value_of_captchaData)
extracted_data2 = (value_of_captchaToken)
-1
elrich bachman 25 Фев 2018 в 20:49

3 ответа

Лучший ответ

Вы можете использовать ast.literal_eval вместо регулярных выражений:

import ast
response_text = '{"captchaData":"/9j/4AAQSkZJRgABAQAAAQABAAD//gATYWJmNjUxYjM1ZjA3ZWRiMgD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy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Sb7QPFun32o2n2eB5w5KsGROeeR+dZ14Sdbna0ujLFU5yxPtGmldHsleEeLbnzfF+pyxN0lKZHsNp/ka9b13xHa6VpzPDKk93IMW8MZ3","captchaMime":"image/jpeg","captchaToken":"ALXfmJpxoaxq6LYBXm-kJzIl0Yd5mHG1XbttsBX-EKxMYtYNIc6uTv89fmRxeWZGEgpi2L9sjXYlkm6Vplav_wy2KjdB5J4j3i5fB6CEuPOMXIjEql6mPBJ8-YJTCpOzzk8kOcW5nuBbuLOdMVyVxquLbWjqLZzHeN0iT4Jm4SIZ9mQNfapNGkE","status":"CAPTCHA"}'
new_data = ast.literal_eval(response_text)
print(new_data["captchaData"])
print(new_data['captchaToken'])
-1
Ajax1234 25 Фев 2018 в 17:51

Если все, что вам нужно, это удалить данный символ до и после строки, вы можете использовать strip

'"some string"'.strip('"') # 'some string'
0
Olivier Melançon 25 Фев 2018 в 17:58

Похоже, что вы на самом деле пытаетесь разобрать JSON. JSON - это формат, который часто используется для представления данных в Интернете.

Если вы используете запросы (как это звучит из тега), вы можете использовать .json() для анализа результата. В противном случае используйте встроенный модуль json.

r = requests.get("https://httpbin.org/get")
data = r.json()

Или

import json

data = json.loads('"key": "value"')
1
Azsgy 25 Фев 2018 в 18:53