如何使用edge浏览器和selenium绕过cloudflare python?

问题描述 投票:0回答:1

我还没有发现任何有关使用 Edge 浏览器绕过 cloudflare 的内容。我正在使用 YouTube mp3 在线下载器,例如 https://yt1ss.pro/en199/youtube-to-mp3。每当我启动 selenium 时,它都会显示 cloudflare 说“正在验证”。然后它说“错误”。我认为它感觉到我正在使用硒。我希望它能够通过并单击所有按钮来下载视频。我也一直在使用 pytubefix 下载视频,但某些 URL (https://music.youtube.com/watch?v=OwONpOi6RYE) 不适用于 pytubefix。我收到此错误:pytubefix.exceptions.VideoUnavailable:OwONpOi6RYE 不可用

如果他们在那里不起作用,那么它将尝试在线下载器。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.edge.options import Options

import time

options = Options()
options.add_argument("--disable-blink-features=AutomationControlled") #I am not exactly sure what this does. I read somewhere that it could help hide the bot fingerprint.
driver = webdriver.Edge(options=options)
driver.get('https://yt1ss.pro/en199/youtube-to-mp3')

url = 'https://music.youtube.com/watch?v=OwONpOi6RYE'


driver.find_element(By.XPATH, '//*[@id="txt-url"]').send_keys(url)
driver.find_element(By.XPATH, '//*[@id="btn-submit"]').click()
driver.implicitly_wait(5)
driver.find_element(By.XPATH, '//*[@id="mp4"]/table/tbody/tr[2]/td[3]/button').click()
driver.implicitly_wait(5)
driver.find_element(By.XPATH, '//*[@id="A_downloadUrl"]').click()

我尝试过其他没有 cloudflare 的下载器,但它们仍然无法工作。他们有弹出广告。我也尝试过无头硒并查看了cloudscraper(https://pypi.org/project/cloudscraper/),尽管不确定这如何与selenium一起使用。

如有任何帮助,我们将不胜感激。

python selenium-webdriver microsoft-edge cloudflare
1个回答
0
投票

好吧,您可以使用 2captcha-python Turnstile 解算器和 API 端点请求 yt1ss。

更新2验证码API

import sys
import os

try:
    from rich import print
    from twocaptcha import TwoCaptcha
    import requests
except ImportError:
    os.system("pip3 install requests rich 2captcha-python -U")
    from rich import print
    from twocaptcha import TwoCaptcha
    import requests


def get_captcha_token():
    """Fetch CAPTCHA token using 2Captcha service."""
    solver = TwoCaptcha(CAPTCHA_API_KEY)
    try:
        result = solver.turnstile(sitekey=CAPTCHA_SITE_KEY, url=CAPTCHA_URL)
        return result["code"]
    except Exception as e:
        print(f">= Error solving CAPTCHA: {e}")
        return None


def fetch_mp3_conversion(CAPTCHA_URL, MUSIC_URL):
    """Fetch MP3 conversion data from yt1ss.pro."""
    captcha_token = get_captcha_token()
    if not captcha_token:
        print(">= Failed to solve CAPTCHA.")
        return None

    headers = {
        "accept": "application/json, text/javascript, */*; q=0.01",
        "accept-language": "en-US,en;q=0.9",
        "cache-control": "no-cache",
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": CAPTCHA_URL,
        "sec-ch-ua": '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
        "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": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
        "x-requested-with": "XMLHttpRequest",
    }

    params = {
        "retry": "undefined",
        "platform": "youtube",
        "mhash": "d503358d68f14ea7",
    }

    data = {
        "url": MUSIC_URL,
        "ajax": "1",
        "lang": "en",
        "cftoken": captcha_token,
    }

    try:
        response = requests.post(
            "https://yt1ss.pro/mates/en/analyze/ajax",
            params=params,
            headers=headers,
            data=data,
        )
        response.raise_for_status()
        return response.json()
    except Exception as e:
        print(f">= Error making request: {e}")
        return None


if __name__ == "__main__":

    root_path = os.path.dirname(os.path.abspath(__file__))
    os.chdir(root_path)

    CAPTCHA_API_KEY = os.getenv("APIKEY_2CAPTCHA", "YOUR_API_KEY")
    CAPTCHA_SITE_KEY = "0x4AAAAAAA2BcqfnaNPkn4HG"
    CAPTCHA_URL = "https://yt1ss.pro/en199/youtube-to-mp3"
    MUSIC_URL = "https://music.youtube.com/watch?v=OwONpOi6RYE"

    result = fetch_mp3_conversion(CAPTCHA_URL, MUSIC_URL)
    if result:
        print("Conversion result:", result)
    else:
        print("Failed to fetch conversion data.")
© www.soinside.com 2019 - 2024. All rights reserved.