Як зібрати cookie cf_clearance із вебсайтів, захищених Cloudflare

Як зібрати cookie cf_clearance із вебсайтів, захищених Cloudflare

In this article, I’ll walk you through the process of scraping the cf_clearance cookie from Cloudflare-protected sites and show you how to use it to overcome Cloudflare’s restrictions, making it easier for you to scrape the data you need.

What is the cf_clearance Cookie?

Cloudflare це популярний сервіс, що забезпечує захист вебсайтів, виявляючи та блокуючи автоматизований трафік. Cloudflare використовує різні методи для ідентифікації ботів, зокрема аналіз IP-адрес, виявлення підозрілих шаблонів поведінки та перевірку запитів за допомогою CAPTCHA або JavaScript-перевірок.

When you visit a Cloudflare-protected website, you often face these challenges, which you must solve before being granted access to the site. Once you successfully pass these challenges, Cloudflare issues a special cookie called cf_clearance. This cookie serves as proof that you have solved the security challenge and allows you to bypass Cloudflare’s protection and access the website.

However, there’s a catch: to scrape the content behind Cloudflare successfully, you need to retrieve this cf_clearance cookie and use it in subsequent requests. If the cookie is missing or expired, Cloudflare will block your scraping attempts.

Why is Scraping the cf_clearance Cookie Important?

When scraping a Cloudflare-protected site, the cf_clearance cookie is essential. If you don’t pass the challenge and retrieve the cookie, your requests will be blocked by Cloudflare. It acts as an access key that allows you to bypass Cloudflare’s security and scrape the data you need.

Once you obtain the cf_clearance cookie, you can use it to authenticate your requests. However, it’s important to note that the cookie is session-based, which means you must maintain the same IP address and User-Agent during the entire scraping session. If you change either, Cloudflare will block your access.

How to Scrape the cf_clearance Cookie?

Scraping the cf_clearance cookie from a Cloudflare-protected website can be tricky since Cloudflare uses several techniques to detect and block bots. In this section, I’ll walk you through how to scrape the cf_clearance cookie using a tool called CF-Clearance-Scraper. This command-line tool automates the process of solving the Cloudflare challenges and retrieving the cf_clearance cookie.

Крок 1: Встановіть CF-Clearance-Scraper

Перший крок — встановити пакет CF-Clearance-Scraper. Цей інструмент створений для роботи з Python 3.10  та запускає екземпляр headless Chrome у фоновому режимі, щоб розв’язувати перевірки Cloudflare.

Клонувати репозиторій: Почніть із клонування репозиторію CF-Clearance-Scraper з GitHub:

git clone https://github.com/Xewdy444/CF-Clearance-Scraper

Встановіть потрібні залежності: Після клонування репозиторію перейдіть до папки та встановіть необхідні залежності за допомогою pip:

cd CF-Clearance-Scraper
pip3 install -r requirements.txt

Now, you’re ready to start scraping the cf_clearance cookie from a Cloudflare-protected website.

Крок 2: Розуміння параметрів CF-Clearance-Scraper

CF-Clearance-Scraper works by running the main.py file with a set of parameters. These parameters control how the tool interacts with Cloudflare’s protection and retrieve the cf_clearance cookie.

Ключові параметри такі:

  • -f: Вказує ім'я вихідного файла, у який cookies буде збережено у форматі JSON.
  • -t: Встановлює тайм-аут запиту в секундах. Це визначає, скільки часу інструмент чекатиме на розв’язання виклику Cloudflare.
  • -p: Визначає URL проксі, який є критично важливим для уникнення виявлення під час вебскрапінгу.
  • -ua: Встановлює заголовок User-Agent для запиту. Важливо використовувати дійсний User-Agent, щоб уникнути виявлення.
  • URL: URL вебсайту, захищеного Cloudflare, який ви хочете скрапити.

Команда для запуску CF-Clearance-Scraper виглядає так:

python main.py -p <PROXY_ADDRESS> -t <TIMEOUT_IN_SECONDS> -ua "<USER_AGENT_STRING>" -f <COOKIE_FILE.json> <TARGET_URL>

Step 3: Scraping the cf_clearance Cookie

To scrape the cf_clearance cookie, you need to run the CF-Clearance-Scraper tool with the appropriate parameters. For example, let’s say you want to scrape the cf_clearance cookie from a Cloudflare-protected page. You would run the following command:

python main.py -p http://190.58.248.86:80 -t 60 -ua "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36" -f cookies.json https://www.scrapingcourse.com/cloudflare-challenge

У цій команді:

  • Proxy: Ми використовуємо проксі, щоб уникнути виявлення. Ви можете використати будь-який проксі на свій вибір, наприклад безкоштовні проксі or преміальні проксі для вебскрапінгу.
  • Timeout: Ми встановлюємо тайм-аут у 60 секунд, тобто інструмент чекатиме до 60 секунд на розв’язання виклику Cloudflare.
  • User-Agent: Ми використовуємо поширений рядок User-Agent, який імітує справжній браузер.
  • Вихідний файл: The cookies will be saved to a file named cookies.json.

Once the command is executed successfully, the tool will print the cf_clearance cookie in the terminal, and all the cookies will be saved in the cookies.json file.

Step 4: Extracting the cf_clearance Cookie Programmatically

While the cf_clearance cookie will be outputted in the terminal, it’s often more convenient to extract it programmatically. You can use Python’s subprocess module to run the CF-Clearance-Scraper tool from within your Python script and extract the cf_clearance cookie using regular expressions.

Here’s an example Python script that runs the CF-Clearance-Scraper tool and extracts the cf_clearance cookie:

import subprocess
import re
def cf_clearance_scraper(url, proxy, user_agent):
command = [
"python",
"main.py",
"-p",
proxy,
"-t",
"60",
"-ua",
user_agent,
"-f",
"cookies.json",
url,
]
try:
# Run the command and capture the output
process = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
output = process.stdout
# Extract the cf_clearance value from the logs using regex
match = re.search(r"cf_clearance=([^s] )", output)
if match:
cf_clearance = match.group(1)
return cf_clearance
else:
return None
except Exception as e:
print(f"Помилка під час виконання команди: {e}")
return None
# Example usage
url = "https://www.scrapingcourse.com/cloudflare-challenge"
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"
proxy = "http://190.58.248.86:80"
cf_clearance = cf_clearance_scraper(url, proxy, user_agent)
if cf_clearance:
print(f"cf_clearance cookie: {cf_clearance}")
else:
print("Failed to retrieve cf_clearance cookie.")

This script runs the CF-Clearance-Scraper tool with the specified parameters, captures the output, and extracts the cf_clearance cookie using a regular expression. If successful, it prints the cookie; otherwise, it prints an error message.

Step 5: Using the cf_clearance Cookie to Bypass Cloudflare

Now that you have the cf_clearance cookie, you can use it to bypass Cloudflare and make scraping requests to the protected website. In this step, we will use the requests library to make HTTP requests while passing the cf_clearance cookie.

Here’s how to integrate the cf_clearance cookie into your scraping code:

import requests
def scraper(url, cf_clearance, proxy, user_agent):
session = requests.Session()
# Set the cf_clearance cookie in the session
session.cookies.set("cf_clearance", cf_clearance)
# Set the User-Agent header
session.headers.update({"User-Agent": user_agent})
# Set the proxy for the session
session.proxies.update(
{
"http": проксі,
"https": proxy,
}
)
try:
# Make the request to the target URL
response = session.get(url)
response.raise_for_status()
return response.text
except requests.exceptions.RequestException as e:
return f"Помилка під час виконання запиту: {e}"
# Example usage
response = scraper(url, cf_clearance, proxy, user_agent)
print(response)

In this code, the scraper function accepts the cf_clearance cookie, along with the target URL, proxy, and User-Agent. It creates a persistent session using the requests.Session() object and sets the cf_clearance cookie, User-Agent, and proxy for the session. The session is then used to make a GET request to the target URL.

Conclusion

By scraping the cf_clearance cookie and using it for your requests, you can bypass Cloudflare’s protection and scrape data from protected websites. While CF-Clearance-Scraper is effective, it has some limitations, including the need for a consistent IP and User-Agent, as well as the risk of cookie expiration during the session.

Щоб зменшити ризики, як-от блокування IP або прострочені cookies, важливо використовувати ротаційні проксі та ефективно керувати сесіями. Такі сервіси, як Bright Data може забезпечити преміальне керування проксі та sticky sessions, допомагаючи підтримувати стабільний доступ до ваших ресурсів. З цими порадами ви зможете ефективніше виконувати вебскрапінг сайтів, захищених Cloudflare.

Схожі записи