How to access the API that posses Cloudflare protection with python.
As you might know that Cloudflare has a DDoS protection mechanism, therefore it is impossible to access the API with Cloudflare protection using traditional requests library.
However, there is a solution to this issue. By using cfscrape, we can requests the data from API with Cloudflare protection.
First of All, you need to download cfscrape. Simply use pip to install it.
pip install cfscrape
After you have successfully install cfscrape, you can start coding your python scripts.
- import the library and create a scraper instance.
import cfscrape
scraper = cfscrape.create_scraper()
2. use the scrape to send a request to the web server in the same manner as requests
req = scraper.get(‘http://pokeapi.co/api/v2/pokemon/384/')
3. Run your script and wait for the result.
That’s all for how to access the API that posses Cloudflare protection with python.
Note: I personally don’t suggest to do this every time you face an API with Cloudflare protection. I just do this tutorial for fun and education purpose. If possible please find another similar API without Cloudflare protection.
Find out more information at: