How to bypass Cloudflare restrictions with Scrapy?

Viewed 5983

The following script ends up throwing the error "Attention Required! | Cloudflare" when I try to get data using response.css('title::text').get() as a test.

Scrapy Spider:

import scrapy

class DataSpider(scrapy.Spider):
    name = "avvo"

    def start_requests(self):
        urls = [
            'https://www.avvo.com/attorneys/84025-ut-jason-hunter-284784/reviews.html',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def __init__(self):
        self.called = False
        
        self.data = {}
        
    def parse(self, response):
        if not self.called:
            self.called = True

            self.data["website"] = response.css('title::text').get()
            
            yield self.data

Result:

'Attention Required! | Cloudflare'
2 Answers

From reading this thread "Attention Required! Message when sharing link" on the Cloudflare Community site, it may be because

... that is what a user would see if they received a captcha challenge when visiting your website

It's probably because your bot is being detected by the site and they are trying to block you. There are ways to reduce the possibility of your bot being detected, search the web and Stackoverflow for solutions; here are some to get you started:

How to scrape the web without getting blocked

How to scrape websites without getting blocked

Advanced Web Scraping Tactics

Cloudflare might be blocking your requests for several reasons. The most probable one is that it requires a Javascript Challenge that your system is not solving.

You'd need a dedicated library cloudscraper or headless browser scrapy-headless to be able to solve them. Any of those will handle internally the whole page load and the challenge. If using you own implementation, you'd probably need to tune it for a better success rate.

Cloudflare also checks other criteria. For example, your IP can give you away, so using good rotating IPs can make a difference.

More information about how Cloudflare works.

Related