Adilzada
Adilzada

Reputation: 63

Scrapy Shell Response 204

i try to parse a specific website : www.bina.az/items/all. And i want to test it before building fully functional spider. So i type scrapy shell bina.az/items/all in terminal and i get this :enter image description here

The reason of this is Cloudfare protection. I know how to bypass cloudfare in scrapy project, but i need to use scrapy shell also.How can i solve this issue ?

Upvotes: 3

Views: 393

Answers (1)

Joaquin
Joaquin

Reputation: 2091

You could run scrapy shell from your project.

Suppose that you have the following project:

cloudfare-spider
    env
    scrapy.cfg
    cloudfare-spider
         __init__.py
         items.py
         middlewares.py
         pipelines.py
         __pycache__
         settings.py
         __init__.py

First go to your project:

cd cloudfare-project

If you don't have virtual env, create one:

virtualenv env

Then activate the virtual env:

source env/bin/activate

Then, in your virtual environment you should install:

pip install scrapy scrapy_cloudflare_middleware 

Then try to run scrapy shell:

>> scrapy shell "https://bina.az/items/all"                                                   
2018-12-02 12:49:24 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: cloudfare)
2018-12-02 12:49:25 [scrapy.utils.log] INFO: Versions: lxml 4.2.5.0, libxml2 2.9.8, cssselect 1.0.3, parsel 1.5.1, w3lib 1.19.0, Twisted 18.9.0, Python 3.7.1 (default, Oct 22 2018, 10:41:28) - [GCC 8.2.1 20180831], pyOpenSSL 18.0.0 (OpenSSL 1.1.0j  20 Nov 2018), cryptography 2.4.2, Platform Linux-4.19.4-arch1-1-ARCH-x86_64-with-arch
2018-12-02 12:49:25 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'cloudfare', 'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter', 'EDITOR': 'vim', 'LOGSTATS_INTERVAL': 0, 'NEWSPIDER_MODULE': 'cloudfare.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['cloudfare.spiders'], 'USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
2018-12-02 12:49:25 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.memusage.MemoryUsage']
2018-12-02 12:49:25 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy_cloudflare_middleware.middlewares.CloudFlareMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2018-12-02 12:49:25 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2018-12-02 12:49:25 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2018-12-02 12:49:25 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2018-12-02 12:49:25 [scrapy.core.engine] INFO: Spider opened
2018-12-02 12:49:26 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://bina.az/robots.txt> (referer: None)
2018-12-02 12:49:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://bina.az/items/all> (referer: None)
[s] Available Scrapy objects:
[s]   scrapy     scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s]   crawler    <scrapy.crawler.Crawler object at 0x7f31a4b652b0>
[s]   item       {}
[s]   request    <GET https://bina.az/items/all>
[s]   response   <200 https://bina.az/items/all>
[s]   settings   <scrapy.settings.Settings object at 0x7f31a4b65630>
[s]   spider     <DefaultSpider 'default' at 0x7f31a463bef0>
[s] Useful shortcuts:
[s]   fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s]   fetch(req)                  Fetch a scrapy.Request and update local objects 
[s]   shelp()           Shell help (print this help)
[s]   view(response)    View response in a browser
In [1]: 

If there is an error when you run scrapy shell try:

deactivate
source env/bin/activate
scrapy shell "https://bina.az/items/all"

As you can see there is the 'scrapy_cloudflare_middleware.middlewares.CloudFlareMiddleware' in [scrapy.middleware]

I also noted that you need to set USER_AGENT to it could work, there is my settings.py file:

BOT_NAME = 'cloudfare'

SPIDER_MODULES = ['cloudfare.spiders']
NEWSPIDER_MODULE = 'cloudfare.spiders'

ROBOTSTXT_OBEY = True

DOWNLOADER_MIDDLEWARES = {
    # The priority of 560 is important, because we want this middleware to kick in just before the scrapy built-in `RetryMiddleware`.
    'scrapy_cloudflare_middleware.middlewares.CloudFlareMiddleware': 560
}

USER_AGENT="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"

Upvotes: 2

Related Questions