Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from selenium import webdriver
- from selenium.webdriver.chrome.options import Options
- def get_cloudflare_cookie(url):
- options = Options()
- options.add_argument("--headless")
- options.add_argument("--disable-gpu")
- options.add_argument("window-size=1920,1080")
- browser = webdriver.Chrome(options=options)
- browser.get(url)
- # You may need to add a waiting mechanism to ensure the challenge is solved
- # For example, using 'time.sleep(seconds)' or 'browser.implicitly_wait(seconds)'
- cookies = browser.get_cookies()
- browser.quit()
- cf_clearance = None
- for cookie in cookies:
- if cookie["name"] == "cf_clearance":
- cf_clearance = cookie["value"]
- break
- return cf_clearance
- url = 'https://example.com/'
- cf_clearance_cookie = get_cloudflare_cookie(url)
- if cf_clearance_cookie:
- session = requests.Session()
- headers = {
- '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',
- }
- session.cookies.set("cf_clearance", cf_clearance_cookie)
- api_url = 'https://example.com/api/your-endpoint'
- headers.update({
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer your_api_key',
- })
- api_response = session.get(api_url, headers=headers)
- if api_response.status_code == 200:
- data = api_response.json()
- print(data)
- else:
- print(f'Error: {api_response.status_code}')
- else:
- print("Failed to bypass Cloudflare.")
Advertisement
Add Comment
Please, Sign In to add comment