python 跳过Cloudflare
Python 2023/1/10 22:02:30 点击:不统计
http://%77%77%77%2E%66%6F%72%61%73%70%2E%63%6E
python 跳过Cloudflare
转自:https://www.yisu.com/zixun/588168.html
CloudFlare 开发的 Python 库 cloudflare-scrape
用上它就可以无感爬取使用了 CloudFlare 的网站,使用这个库非常简单。
安装
使用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.decode('utf-8')) # 如果是其他编码,自行修改
处理 post 请求的 CloudFlare
# import cfscrape
# 实例化一个create_scraper对象
scraper = cfscrape.create_scraper()
# 获取真实网页源代码
web_data = scraper.post("http://example.com").content
print(web_data.decode('utf-8')) # 如果是其他编码,自行修改
使用cloudflare-scrape后整个请求过程如丝般顺滑。