python 5秒盾
在做网页自动化的时候用python启动网页输入网址,经常被盾,不能正常进入
这里有是那种解决方式,https://localcoder.org/selenium-stuck-on-checking-your-browser-before-accessing-url
方法1:
from selenium import webdriver
url = "<WEBSITE>"
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.get(url)
还需要修改:
perl -pi -e 's/cdc_/dog_/g' /path/to/chromedriver
程序需要修改:
import undetected_chromedriver as uc
url = "<WEBSITE>"
driver= uc.Chrome()
driver.get(url)
方法2:
这是因为浏览器使用cloudfare保护自己免受DDOS(分布式拒绝服务)攻击。有两种方法可以解决这个问题:
如果网页加载需要5秒钟,只需使用时间。
使用WebDriverWait——例如,id为“sample btn”的按钮仅显示在此屏幕之后。那么你能做的就是:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
btn = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'sample-btn'))) #Web driver waits for 10 seconds until element is visible
如果方法2好用,推荐2.
方法3:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
# Configure browser
options = webdriver.ChromeOptions()
options.add_argument(f"--user-data-dir=C:\\Users\\daria\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("--disable-blink-features=AutomationControlled")
chromedriver = ChromeDriverManager(chrome_type=ChromeType.GOOGLE,
log_level='0',
print_first_line=False).install()
driver = webdriver.Chrome(chromedriver,
options=options,
service_log_path=None)
input ("End?")
先说说这个按照正常流程是怎么实现抓取绕过的:
使用浏览器模拟技术请求目标网站,例如:Selenium、 PhantomJS等
破解请求 2 的加密参数使用请求库模拟整个请求过程
这两个方法当然是可以抓取的,但是都不符合标题的巧字。
接下来给大家介绍一个专门为了绕过这个 CloudFlare 开发的 Python 库 cloudflare-scrape
用上它就可以无感爬取使用了 CloudFlare 的网站,使用这个库非常简单。
安装
使用pip install cfscrape安装cloudflare-scrape,同时确认本地是否安装node.js开发环境,如果没有,需要安装配置nodejs开发环境。
能过5秒盾,还能加载谷歌浏览器的插件
实际上,要绕过这个5秒盾非常简单,只需要使用一个第三方库,叫做cloudscraper。我们可以使用pip来安装:
python3 -m pip install cloudscraper
安装完成以后,只需要使用3行代码就能绕过 Cloud Flare 的5秒盾:
import cloudscraper
scraper = cloudscraper.create_scraper()
resp = scraper.get('目标网站').text
我们还是以上面的网站为例:
import cloudscraper
from lxml.html import fromstring
scraper = cloudscraper.create_scraper()
resp = scraper.get('https://mv-voice.com/news/2021/05/04/mountain-view-whisman-students-sent-home-after-children-test-positive-for-covid-19').text
selector = fromstring(resp)
title = selector.xpath('//h1/text()')[0]
print(title)
运行效果如下图所示:
破盾成功。
CloudScraper[2] 非常强大,它可以突破 Cloud Flare 免费版各个版本的五秒盾。而且它的接口和 requests 保持一致。原来用 requests 怎么写代码,现在只需要把requests.xxx改成scraper.xxx就可以了。
httpx 可以很轻松绕过 cloudflare, 使用http2.0的协议就行了。
client = httpx.Client(http2=True, verify=False)
res = client.get(url, headers=headers)
好家伙是python爬虫吧
想要过5秒盾,还能加载谷歌浏览器的插件
我推荐这款插件
Xpath helper Python爬虫工具谷歌浏览器插件_xudailong_blog的博客-CSDN博客_google的xpath插件 https://blog.csdn.net/xudailong_blog/article/details/78837538
下载库
pip install cfscrape
使用案例
import cfscrape
# 实例化一个create_scraper对象
scraper = cfscrape.create_scraper()
# 请求报错,可以加上时延
# scraper = cfscrape.create_scraper(delay = 10)
# 获取网页源代码
web_data = scraper.post("http://example.com").content
print(web_data)
利用cloudscraper,它的安装和使用网上一大堆,可以自己去找一下,学一下
使用pip install cfscrape安装cloudflare-scrape,同时确认本地是否安装node.js开发环境,如果没有,需要安装配置nodejs开发环境。
使用实例
处理 get 请求的 CloudFlare
import cfscrape
# 实例化一个create_scraper对象
scraper = cfscrape.create_scraper()
# 请求报错,可以加上时延
# scraper = cfscrape.create_scraper(delay = 10)
# 获取网页源代码
web_data = scraper.get("https://wallhere.com/").content
print(web_data)
处理 post 请求的 CloudFlare
# import cfscrape
# 实例化一个create_scraper对象
scraper = cfscrape.create_scraper()
# 获取真实网页源代码
web_data = scraper.post("http://example.com").content
print(web_data)
安装
使用pip install cfscrape安装cloudflare-scrape,同时确认本地是否安装node.js开发环境,如果没有,需要安装配置nodejs开发环境。
使用实例
处理 get 请求的 CloudFlare
import cfscrape
# 实例化一个create_scraper对象
scraper = cfscrape.create_scraper()
# 请求报错,可以加上时延
# scraper = cfscrape.create_scraper(delay = 10)
# 获取网页源代码
web_data = scraper.get("https://wallhere.com/").content
print(web_data)
处理 post 请求的 CloudFlare
# import cfscrape
# 实例化一个create_scraper对象
scraper = cfscrape.create_scraper()
# 获取真实网页源代码
web_data = scraper.post("http://example.com").content
print(web_data)