Package: cfscrape
cloudflare-scrapeA simple Python module to bypass Cloudflare's anti-bot page (also known as "I'm Under Attack Mode", or IUAM), implemented withRequests. Python versions 2.6 - 3.7 are supported. Cloudflare changes their techniques periodically, so I will update this repo frequently.This can be useful if you wish to scrape or crawl a website protected with Cloudflare. Cloudflare's anti-bot page currently just checks if the client supports JavaScript, though they may add additional techniques in the future.Due to Cloudflare continually changing and hardening their protection page, cloudflare-scrape requires Node.js to solve JavaScript challenges. This allows the script to easily impersonate a regular web browser without explicitly deobfuscating and parsing Cloudflare's JavaScript.Note: This only works when regular Cloudflare anti-bots is enabled (the "Checking your browser before accessing..." loading page). If there is a reCAPTCHA challenge, you're out of luck. Thankfully, the JavaScript check page is much more common.For reference, this is the default message Cloudflare uses for these sorts of pages:Checking your browser before accessing website.com.

This process is automatic. Your browser will redirect to your requested content shortly.

Please allow up to 5 seconds...Any script using cloudflare-scrape will sleep for 5 seconds for the first visit to any site with Cloudflare anti-bots enabled, though no delay will occur after the first request.InstallationSimply runpip install cfscrape. You can upgrade withpip install -U cfscrape. The PyPI package is athttps://pypi.python.org/pypi/cfscrape/Alternatively, clone this repository and runpython setup.py install.Node.js dependencyNode.jsversion 10 or above is required to interpret Cloudflare's obfuscated JavaScript challenge.Your machine may already have Node installed (check withnode -v). If not, you can install it withapt-get install nodejson Ubuntu >= 18.04 and Debian >= 9 andbrew install nodeon macOS. Otherwise, you can get it fromNode's download pageortheir package manager installation page.UpdatesCloudflare regularly modifies their anti-bot protection page and improves their bot detection capabilities.If you notice that the anti-bot page has changed, or if this module suddenly stops working, please create a GitHub issue so that I can update the code accordingly.Many issues are a result of users not updating to the latest release of this project. Before filing an issue, please run the following command to update cloudflare-scrape to the latest version:pip install -U cfscrapeIf you are still encountering a problem, create a GitHub issue and please include:The version number frompip show cfscrape.The relevant code snippet that's experiencing an issue or raising an exception.The full exception and traceback, if applicable.The URL of the Cloudflare-protected page which the script does not work on.A Pastebin or Gist containing the HTML source of the protected page.If you've upgraded and are still experiencing problems,click here to create a GitHub issue and fill out the pertinent information.UsageThe simplest way to use cloudflare-scrape is by callingcreate_scraper().importcfscrapescraper=cfscrape.create_scraper()# returns a CloudflareScraper instance# Or: scraper = cfscrape.CloudflareScraper()  # CloudflareScraper inherits from requests.Sessionprintscraper.get("http://somesite.com").content# => "<!DOCTYPE html><html><head>..."That's it. Any requests made from this session object to websites protected by Cloudflare anti-bot will be handled automatically. Websites not using Cloudflare will be treated normally. You don't need to configure or call anything further, and you can effectively treat all websites as if they're not protected with anything.You use cloudflare-scrape exactly the same way you use Requests. (CloudflareScraperworks identically to a RequestsSessionobject.) Just instead of callingrequests.get()orrequests.post(), you callscraper.get()orscraper.post(). ConsultRequests' documentationfor more information.OptionsExisting sessionIf you already have an existing Requests session, you can pass it tocreate_scraper()to continue using that session.session=requests.session()session.headers=...scraper=cfscrape.create_scraper(sess=session)Unfortunately, not all of Requests' session attributes are easily transferable, so if you run into problems with this, you should replace your initialsess = requests.session()call withsess = cfscrape.create_scraper().DelaysNormally, when a browser is faced with a Cloudflare IUAM challenge page, Cloudflare requires the browser to wait 5 seconds before submitting the challenge answer. If a website is under heavy load, sometimes this may fail. One solution is to increase the delay (perhaps to 10 or 15 seconds, depending on the website). If you would like to override this delay, pass thedelaykeyword argument tocreate_scraper()orCloudflareScraper().There is no need to override this delay unless cloudflare-scrape generates an error recommending you increase the delay.scraper=cfscrape.create_scraper(delay=10)IntegrationIt's easy to integrate cloudflare-scrape with other applications and tools. Cloudflare uses two cookies as tokens: one to verify you made it past their challenge page and one to track your session. To bypass the challenge page, simply include both of these cookies (with the appropriate user-agent) in all HTTP requests you make.To retrieve just the cookies (as a dictionary), usecfscrape.get_tokens(). To retrieve them as a fullCookieHTTP header, usecfscrape.get_cookie_string().get_tokensandget_cookie_stringboth accept Requests' usual keyword arguments (likeget_tokens(url, proxies={"http": "socks5://localhost:9050"})). Please readRequests' documentation on request argumentsfor more information.User-Agent HandlingThe two integration functions return a tuple of(cookie, user_agent_string).You must use the same user-agent string for obtaining tokens and for making requests with those tokens, otherwise Cloudflare will flag you as a bot.That means you have to pass the returneduser_agent_stringto whatever script, tool, or service you are passing the tokens to (e.g. curl, or a specialized scraping tool), and it must use that passed user-agent when it makes HTTP requests.If your tool already has a particular user-agent configured, you can make cloudflare-scrape use it withcfscrape.get_tokens("http://somesite.com/", user_agent="User-Agent Here")(also works forget_cookie_string). Otherwise, a randomly selected user-agent will be used.Integration examplesRemember, you must always use the same user-agent when retrieving or using these cookies. These functions all return a tuple of(cookie_dict, user_agent_string).Retrieving a cookie dict through a proxyget_tokensis a convenience function for returning a Python dict containing Cloudflare's session cookies. For demonstration, we will configure this request to use a proxy. (Please note that if you request Cloudflare clearance tokens through a proxy, you must always use the same proxy when those tokens are passed to the server. Cloudflare requires that the challenge-solving IP and the visitor IP stay the same.)If you do not wish to use a proxy, just don't pass theproxieskeyword argument. These convenience functions support all of Requests' normal keyword arguments, likeparams,data, andheaders.importcfscrapeproxies={"http":"http://localhost:8080","https":"http://localhost:8080"}tokens,user_agent=cfscrape.get_tokens("http://somesite.com",proxies=proxies)printtokens# => {'cf_clearance': 'c8f913c707b818b47aa328d81cab57c349b1eee5-1426733163-3600', '__cfduid': 'dd8ec03dfdbcb8c2ea63e920f1335c1001426733158'}Retrieving a cookie stringget_cookie_stringis a convenience function for returning the tokens as a string for use as aCookieHTTP header value.This is useful when crafting an HTTP request manually, or working with an external application or library that passes on raw cookie headers.importcfscraperequest="GET / HTTP/1.1\r\n"cookie_value,user_agent=cfscrape.get_cookie_string("http://somesite.com")request+="Cookie:%s\r\nUser-Agent:%s\r\n"%(cookie_value,user_agent)printrequest# GET / HTTP/1.1\r\n# Cookie: cf_clearance=c8f913c707b818b47aa328d81cab57c349b1eee5-1426733163-3600; __cfduid=dd8ec03dfdbcb8c2ea63e920f1335c1001426733158# User-Agent: Some/User-Agent Stringcurl exampleHere is an example of integrating cloudflare-scrape with curl. As you can see, all you have to do is pass the cookies and user-agent to curl.importsubprocessimportcfscrape# With get_tokens() cookie dict:# tokens, user_agent = cfscrape.get_tokens("http://somesite.com")# cookie_arg = "cf_clearance=%s; __cfduid=%s" % (tokens["cf_clearance"], tokens["__cfduid"])# With get_cookie_string() cookie header; recommended for curl and similar external applications:cookie_arg,user_agent=cfscrape.get_cookie_string("http://somesite.com")# With a custom user-agent string you can optionally provide:# ua = "Scraping Bot"# cookie_arg, user_agent = cfscrape.get_cookie_string("http://somesite.com", user_agent=ua)result=subprocess.check_output(["curl","--cookie",cookie_arg,"-A",user_agent,"http://somesite.com"])Trimmed down version. Prints page contents of any site protected with Cloudflare, via curl. (Warning:shell=Truecan be dangerous to use withsubprocessin real code.)url="http://somesite.com"cookie_arg,user_agent=cfscrape.get_cookie_string(url)cmd="curl --cookie{cookie_arg}-A{user_agent}{url}"print(subprocess.check_output(cmd.format(cookie_arg=cookie_arg,user_agent=user_agent,url=url),shell=True))