Manually solve captcha in a request session

Viewed 243

I have a software in python that use post request to reach an API from an external website.

Sometimes, instead of getting the result of my request, I got an URL with a captcha to solve.

Here the code of the requests

headers= {}
headers['Accept']= 'application/json,application/hal+json'
headers['Content-Type']= 'application/json; charset=UTF-8'
headers['Connection']= 'Keep-Alive'

url = 'https://api.website.com/v1/price'
data = [{'category':int(category_id)}]
r = requests.session()
r = requests.post(url, headers=headers, data=json.dumps(data))

So far it's OK, then I get all the date in the following variable and print then in the software using Tkinter:

json_result = json.loads(r.text)

Unfortunately, sometimes the API returns an URL with a link to solve a captcha

{"url":"https://geo.captcha-delivery.com/captcha/?initialCid=AHrlqAAAMAwIhGhOSjsfUAsLEFTg==&cid=Y.M~8XWYwAHts6n_H32O8fm~WTeMw.1cDlRGOH16q4PhWtuo8Xm~KgWeW6d1jQptRljywWkJHFMu9IgEZYRheo3OPww6XjgqXQcs1X1m&referer=https%3A%2F%2Fapi.website.com&hash=05B30BD905598BD2EE8F5A199D973&t=fe&s=575"}

If I open this url in a browser, I can solve the captcha but then I broke the chain so the software is unable to continues.

How could I open in python the url, solve the captcha and when it is solved, continue the request to get the element ?

EDIT : the solution must store the cookie in the request session

Thank you

0 Answers
Related