Skip to content

Instantly share code, notes, and snippets.

@PBadicean
Last active May 17, 2024 06:28
Show Gist options
  • Save PBadicean/1c0c5daec3e1e4a02ffac0bf21d8373b to your computer and use it in GitHub Desktop.
Save PBadicean/1c0c5daec3e1e4a02ffac0bf21d8373b to your computer and use it in GitHub Desktop.
Solving Datadome example
import json
import time
import requests
proxy = "your_proxy_here"
url = "https://www.example.com/"
my_key = "your_2captcha_api_key_here"
user_agent = 'your_user_agent_here'
# Read cookies from the file
with open("cookies_datadome.json", 'r') as json_file:
cookie_value = json.load(json_file)
cookies = {'datadome': cookie_value}
# Prepare request headers
headers = {
'accept': '*/*',
'accept-language': 'en-US',
'content-type': 'text/plain;charset=UTF-8',
'origin': url,
'referer': url,
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': user_agent,
}
# Send GET request to the website
res = requests.get(url, proxies={'http': 'http://' + proxy}, cookies=cookies, headers=headers)
if res.status_code == 403:
# Extract data from the response
dd = res.text.split('dd=')[1].split('</script')[0]
dd = json.loads(dd.replace("'", '"'))
cid = res.headers.get('Set-Cookie').split('datadome=')[1].split(';')[0]
# Form the captcha URL
cap_url = f"https://geo.captcha-delivery.com/captcha/?initialCid={dd['cid']}&hash={dd['hsh']}&cid={cid}&t={dd['t']}&referer=https%3A%2F%2Fwww.example.com%2Fapi%2Fgraphql&s={dd['s']}&e={dd['e']}"
print('captcha url:', cap_url)
# Send request to 2Captcha
data = {
"key": my_key,
"method": "datadome",
"captcha_url": cap_url,
"pageurl": url,
"json": 1,
"userAgent": user_agent,
"proxy": proxy,
"proxytype": "http",
}
response = requests.post("https://2captcha.com/in.php", data=data)
s = response.json()["request"]
# Wait for captcha solution
while True:
solu = requests.get(f"https://2captcha.com/res.php?key={my_key}&action=get&json=1&id={s}").json()
if solu["request"] == "CAPCHA_NOT_READY":
time.sleep(5)
elif "ERROR" in solu["request"]:
print(solu["request"])
exit(0)
else:
break
# Update cookies and save to the file
cookie_value = solu["request"].split(";")[0].split("=")[1]
with open("cookies_datadome.json", 'w') as json_file:
json.dump(cookie_value, json_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment