-2

I wrote my first crawler trying to get information from a custom watchlist on coinmarketcap.com.

  1. scrape all 255 assets in the watchlist
  2. scrape all 255 assets and retrieve information: name, marketcap, volume, watchlist

Point is, I am not capable of getting all 255 assets, only 200 of them at best due to 429 HTTP status.

I have used following measures in settings.py:

PROXY_POOL_ENABLED = True
DOWNLOADER_MIDDLEWARES = {
    # ...
    'scrapy_proxy_pool.middlewares.ProxyPoolMiddleware': 610,
    'scrapy_proxy_pool.middlewares.BanDetectionMiddleware': 620,
    # ...
}
AUTOTHROTTLE_ENABLED = True

Can someone help me? Very much appreciated!

import scrapy
import pandas as pd
from scrapy.crawler import CrawlerProcess
process = CrawlerProcess()

names = []
marketcap = []
volume = []
watchlists = []

target_url = "https://coinmarketcap.com/watchlist/614c5fd5c76244179e4f5e75/"

class CryptoCrawler(scrapy.Spider):
    name = 'cryptocrawler'
    
    def start_requests( self ):
        yield scrapy.Request(url = target_url, callback = self.parse_front)
    
    def parse_front(self, response):
        slugs = response.css('tr > td:nth-of-type(3) a::attr(href)').extract()
        newSearch = ['https://coinmarketcap.com' + i for i in slugs]
        for url in newSearch:
            yield response.follow(url = url, callback = self.parse_pages)
        
    def parse_pages(self, response):                    
        names.append(response.css('h2.sc-1q9q90x-0::text').extract_first())
        marketcap.append(response.css('div.statsValue::text').extract_first())
        volume.append(response.xpath('//tr[4]/td/span/text()').extract())
        watchlists.append(response.xpath('//div[@class="namePill"]/text()').extract()[1])`
        
process.crawl(CryptoCrawler)
process.start()

Here is the output:

2021-10-17 12:33:33 [scrapy.core.engine] INFO: Closing spider (finished)
2021-10-17 12:33:33 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/exception_count': 43,
 'downloader/exception_type_count/twisted.internet.error.TCPTimedOutError': 43,
 'downloader/request_bytes': 153962,
 'downloader/request_count': 550,
 'downloader/request_method_count/GET': 550,
 'downloader/response_bytes': 13995423,
 'downloader/response_count': 507,
 'downloader/response_status_count/200': 201,
 'downloader/response_status_count/301': 1,
 'downloader/response_status_count/302': 1,
 'downloader/response_status_count/429': 304,
 'elapsed_time_seconds': 177.913973,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2021, 10, 17, 10, 33, 33, 906232),
 'httperror/response_ignored_count': 28,
 'httperror/response_ignored_status_count/429': 28,
 'log_count/DEBUG': 523,
 'log_count/ERROR': 82,
 'log_count/INFO': 40,
 'request_depth_max': 1,
 'response_received_count': 229,
 'retry/count': 292,
 'retry/max_reached': 55,
 'retry/reason_count/429 Unknown Status': 276,
 'retry/reason_count/twisted.internet.error.TCPTimedOutError': 16,
 'scheduler/dequeued': 550,
 'scheduler/dequeued/memory': 550,
 'scheduler/enqueued': 550,
 'scheduler/enqueued/memory': 550,
 'start_time': datetime.datetime(2021, 10, 17, 10, 30, 35, 992259)}
2021-10-17 12:33:33 [scrapy.core.engine] INFO: Spider closed (finished)

1 Answers1

0

Whenever you run a spider from a script using CrawlerProcess you need to explicitly pass the settings object as below. If you don't the crawler will ignore the values in the settings.py file.

process = CrawlerProcess(settings={
        'PROXY_POOL_ENABLED': True,
        'DOWNLOADER_MIDDLEWARES': {
            # ...
            'scrapy_proxy_pool.middlewares.ProxyPoolMiddleware': 610,
            'scrapy_proxy_pool.middlewares.BanDetectionMiddleware': 620,
        }, 
        'AUTOTHROTTLE_ENABLED': True, })
process.crawl(CryptoCrawler)
process.start()

A convenience function exists when working in a project and helps you import the settings defined in the settings.py file. Add the import from scrapy.utils.project import get_project_settings and define the crawler process as process = CrawlerProcess(get_project_settings())

See the scrapy docs for more information on using CrawlerProcess

msenior_
  • 1,913
  • 2
  • 11
  • 13