Redirect link to run a python script

Solution for Redirect link to run a python script
is Given Below:

On every outboud links on post, when I click on links, I want to get the final url from a clickable link, then replace some elements on the final url , then go to the final links.
For example:
The original link on post: https://example.com/go/771/
when click on the link, it will redirect to :
https://www.exampledomain.com/?aid=318dd9f8&data1=9gxijwksq68v36sv__53:VT13100210635792
I want to click anytime on original link and it will redirect to run a python script to replace character: 318dd9f8 by 234324 ( for example), so final link i want is :
https://www.exapledomain.com/?id=234324&data1=9gxijwksq68v36sv__53:VT13100210635792

How I can do this by python?
I’m a beginner.