Guest User

dermo

a guest
Mar 28th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. import requests
  2. from selenium import webdriver
  3. from selenium.webdriver.chrome.options import Options
  4.  
  5. def get_cloudflare_cookie(url):
  6.     options = Options()
  7.     options.add_argument("--headless")
  8.     options.add_argument("--disable-gpu")
  9.     options.add_argument("window-size=1920,1080")
  10.    
  11.     browser = webdriver.Chrome(options=options)
  12.     browser.get(url)
  13.    
  14.     # You may need to add a waiting mechanism to ensure the challenge is solved
  15.     # For example, using 'time.sleep(seconds)' or 'browser.implicitly_wait(seconds)'
  16.    
  17.     cookies = browser.get_cookies()
  18.     browser.quit()
  19.    
  20.     cf_clearance = None
  21.     for cookie in cookies:
  22.         if cookie["name"] == "cf_clearance":
  23.             cf_clearance = cookie["value"]
  24.             break
  25.    
  26.     return cf_clearance
  27.  
  28. url = 'https://example.com/'
  29. cf_clearance_cookie = get_cloudflare_cookie(url)
  30.  
  31. if cf_clearance_cookie:
  32.     session = requests.Session()
  33.    
  34.     headers = {
  35.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
  36.     }
  37.    
  38.     session.cookies.set("cf_clearance", cf_clearance_cookie)
  39.    
  40.     api_url = 'https://example.com/api/your-endpoint'
  41.     headers.update({
  42.         'Content-Type': 'application/json',
  43.         'Authorization': 'Bearer your_api_key',
  44.     })
  45.    
  46.     api_response = session.get(api_url, headers=headers)
  47.    
  48.     if api_response.status_code == 200:
  49.         data = api_response.json()
  50.         print(data)
  51.     else:
  52.         print(f'Error: {api_response.status_code}')
  53. else:
  54.     print("Failed to bypass Cloudflare.")
  55.  
Advertisement
Add Comment
Please, Sign In to add comment