Last active
May 14, 2024 13:21
-
-
Save i-sync/2f9c9c62c340498252cf3acfe6983dcf to your computer and use it in GitHub Desktop.
Python use selenium skip cloudflare Verify you are human
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this works. | |
const puppeteer = require('puppeteer'); | |
const fs = require('fs') | |
const path = require('path') | |
const { promisify } = require('util') | |
const readFileAsync = promisify(fs.readFile) | |
const writeFileAsync = promisify(fs.writeFile); | |
const getBasenameFormUrl = (urlStr) => { | |
const url = new URL(urlStr) | |
return path.basename(url.pathname) | |
} | |
(async () => { | |
const url = 'http://static12.hentai-cosplays.com/upload/20230705/335/342298/6.jpg'; | |
const browser = await puppeteer.launch({ headless: false }); | |
const page = await browser.newPage(); | |
await page.setViewport({ width: 1200, height: 800 }) | |
await page.goto(url); | |
//await page.screenshot({ path: 'example.png' }); | |
// div | |
//await page.waitForSelector("#challenge-stage") | |
console.log('waiting for iframe with form to be ready.'); | |
await page.waitForSelector('iframe'); | |
console.log('iframe is ready. Loading iframe content'); | |
const elementHandle = await page.$('iframe',); | |
const iframe = await elementHandle.contentFrame(); | |
//var frames = await page.frames(); | |
//var iframe = frames.find(f => f.url().indexOf("challenges.cloudflare.com") > -1); | |
await iframe.waitForSelector('label.ctp-checkbox-label input'); | |
await iframe.click('label.ctp-checkbox-label input'); | |
page.on('response', async (response) => { | |
const matches = /.*\.(jpg|png|svg|gif)$/.exec(response.url()); | |
console.log(matches); | |
if (matches && (matches.length === 2)) { | |
const extension = matches[1]; | |
const filename = getBasenameFormUrl(response.url()); | |
const buffer = await response.buffer(); | |
fs.writeFileSync(`images/${filename}`, buffer, 'base64'); | |
} | |
}); | |
await page.waitForTimeout(3000); | |
await page.waitForNavigation(); | |
await page.waitForNetworkIdle(); | |
await browser.close(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this not worked. | |
import time | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.chrome.service import Service | |
from selenium.webdriver.support.wait import WebDriverWait | |
# Set up the browser | |
options = webdriver.ChromeOptions() | |
#options = webdriver.FirefoxOptions() | |
#options.add_argument('headless') | |
#service = Service(executable_path='c:\path\geckodriver.exe') | |
driver = webdriver.Chrome(options=options) | |
#driver = webdriver.Firefox(service=service, options=options) | |
url = 'https://static6.hentai-cosplays.com/upload/20211215/257/262579/1.jpg' | |
# Navigate to the website | |
driver.get(url) | |
# Wait for the Cloudflare challenge to appear | |
#challenge = driver.find_element(By.CSS_SELECTOR , "#challenge-stage") | |
el = WebDriverWait(driver, timeout=10).until(lambda d: d.find_element(By.CSS_SELECTOR , "#challenge-stage")) | |
# 存储网页元素 | |
iframe = driver.find_element(By.CSS_SELECTOR, "#turnstile-wrapper iframe") | |
# 切换到选择的 iframe | |
driver.switch_to.frame(iframe) | |
# Solve the challenge | |
# For example, if the challenge is a CAPTCHA, you could use the following code to solve it: | |
captcha = WebDriverWait(driver, timeout=10).until(lambda d: d.find_element(By.CSS_SELECTOR , "label.ctp-checkbox-label input")) | |
captcha.click() | |
# ... code to solve the CAPTCHA ... | |
#submit_button = driver.find_element_by_xpath("//button[@class='submit-button']") | |
#submit_button.click() | |
time.sleep(10) | |
# Get the content of the website | |
content = driver.page_source | |
print(content) | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment