0%

绕过Cloudflare反爬虫机制的五种方法

绕过Cloudflare反爬虫机制的五种方法

  • 本文将介绍五种方法,帮助开发者绕过Cloudflare反爬虫机制,包括使用cloudscraper库、抓取谷歌缓存、使用undetected_chromedriver库、使用付费代理和使用穿云API。

  • 在爬虫开发中,有时候会遇到浏览器正常访问网站,但是代码却无法获取数据,返回403等错误。以下是解决这个问题的五种方法:

img

方法一:cloudscraper

对于需要等待的网站(通常等待5秒钟),80%的情况下可以确定使用了5秒盾反爬机制。在Python中,可以使用cloudscraper库绕过等待。

  • 使用方法:
# 安装:cloudscraper
pip install cloudscraper

# 更新最新版本:
pip install cloudscraper -U  
  • 普通使用:
import cloudscraper

scraper=cloudscraper.create_scraper() # 创建实例
res=scraper.get(url) # 请求URL
print(res.text) # 打印结果
  • 在Scrapy中使用:
middlewares.py
import cloudscraper
class CloudScraperMiddleware:
defprocess_response(self,request,response,spider):
ifresponse.status == 403:
url = request.url
req = spider.scraper.get(url,headers={‘referer’:url})
return HtmlResponse(url = url,body = req.text, encoding = ”utf-8″, request = request)
return response

spider.py

importcloudscraper
#启用中间件
custom_settings={
”DOWNLOADER_MIDDLEWARES”:{
’testspider.middlewares.CloudScraperMiddleware’:520,
}
}
def__init__(self,**kwargs):
#创建实例
self.scraper=cloudscraper.create_scraper()  

方法二:抓取谷歌缓存

当谷歌抓取网络以索引网页时,会创建一个缓存。大多数受Cloudflare保护的网站都允许谷歌抓取其网页,因此我们可以抓取这个缓存。

  • 使用方法:
import requests

url = "https://www.xxx.com/"
new_url = f"https://webcache.googleusercontent.com/search?q=cache:{url}"
response = requests.get(new_url)
print(response.text)
#提取所需数据  

方法三:undetected_chromedriver

如果您使用Selenium抓取网页时被封禁,可以尝试使用undetected_chromedriver库。该库是一个自动化工具,更简单且不容易被封禁,甚至无需下载驱动。

  • 安装方法:
pip3 install undetected-chromedriver
  • 使用方法:
import undetected_chromedriverasuc
url = "https://www.baidu.com/"
driver = uc.Chrome()
driver.get(url)

方法四:使用付费代理

目前有很多成熟的代理服务可供使用。通过使用付费代理,可以有效绕过Cloudflare的反爬机制。请选择合适的代理服务商,并根据提供的API密钥设置代理。

  • 使用方法:
import requests

url = "https://xxxx.com/"
api_key = "一长串密钥"
proxy=f"http://{api_key}:@proxy.zenrows.com:8001"
proxies={"http": proxy, "https": proxy}
response=requests.get(url, proxies=proxies, verify=False)
#处理响应数据

方法五:使用穿云API

  穿云API是一个可以绕过Cloudflare的反机器人验证、CAPTCHA验证、WAF和CC防护的解决方案。它提供了HTTPAPI和代理功能,并可设置Referer、浏览器UA和headless状态等浏览器指纹设备特征。

  使用穿云API可以轻松绕过Cloudflare验证,即使需要发送大量请求也不必担心被识别为抓取者。