Как поместить несколько пауков в один файл - PullRequest
0 голосов
/ 19 июня 2019

Я добавляю второго паука в мою программу scrapy, и я хотел бы поместить его в тот же файл python, что и мой другой паук, и использовать те же настройки и все такое, но у меня возникают проблемы с определением этого, потому что, когда я иду, чтобы создатьновый паук создает новые настройки и так далее для нового паука.

# Spider class
class MySpider(Spider):
    # Name of Spider
    name = 'splash_spider'
    # getting all the url + ip address + useragent pairs then request them

    def start_requests(self):
        # get the file path of the csv file that contains the pairs from the settings.py
        with open(self.settings["PROXY_CSV_FILE"], mode="r") as csv_file:
            # requests is a list of dictionaries like this -> {url: str, ua: str, ip: str}

            requests = process_csv(csv_file)
            for i, req in enumerate(requests):
                x = len(requests) - i
                # Return needed url with set delay of 3 seconds
                yield SplashRequest(url=req["url"], callback=self.parse, args={"wait": 3},
                    # Pair with user agent specified in csv file
                    headers={"User-Agent": req["ua"]},
                    # Sets splash_url to whatever the current proxy that goes with current URL  is instead of actual splash url
                    splash_url = req["ip"],
                    priority = x,
                    meta={'priority': x}  # <- check here!!
                    )

    # Scraping function that will scrape URLs for specified information
    def parse(self, response):
       # parse for first spider

#class LoginSpider(scrapy.Spider):
   name = 'login_spider'
   my_urls  = ['https://www.starcitygames.com/myaccount/']
   def start_requests(self):
       for url in self.my_urls:
           yield Request(url, meta={'proxy': 'http://199.89.192.97::8050'})

   def parse(self, response):
       # parse for second spider

Ответы [ 2 ]

0 голосов
/ 19 июня 2019

В моем файле настроек есть глобальная настройка параметров, а затем я обновляю эту настройку с помощью пользовательских настроек для каждого паука.

settings.py

global main_settings
main_settings = {
    'ITEM_PIPELINES': {
        'pipelines.MainPipeline': 90,
    },
    'CONCURRENT_REQUESTS': 100,
    'CONCURRENT_REQUESTS_PER_IP': 100,
    'ROBOTSTXT_OBEY': False,
    'CONCURRENT_ITEMS': 300,
    'REACTOR_THREADPOOL_MAXSIZE': 150,
    'LOG_LEVEL': 'INFO',
    'RETRY_ENABLED': False,
    'DONT_RETY': True,
    'RETRY_TIMES': 0,
    'COOKIES_ENABLED': False,
    'REDIRECT_MAX_TIMES': 0,
    'DOWNLOAD_FAIL_ON_DATALOSS': False,
    'DNS_TIMEOUT': 60,
    'LOG_STDOUT': True,
    'DOWNLOADER_STATS': False
}

spiders.py

import settings

class MySpider(Spider):
    name = 'my_spider'
    custom_settings = dict(settings.main_settings)
    local_settings = {
        'FEED_EXPORT_FIELDS': ["url"],
        'FEED_FORMAT': 'csv',
        'FEED_URI': './output/phase3.csv'
    }
    custom_settings.update(local_settings)
0 голосов
/ 19 июня 2019

Единственный способ сделать это - иметь класс BaseSpider и выбрать там custom_settings по вашему выбору, а затем создать 2 пауков, которые наследуют от этого BaseSpider

class BaseSpider(scrapy.Spider):

    custom_settings = {
        'CONCURRENT_REQUESTS': 100
        # and other settings
    }


class MySpider(BaseSpider):
    # Name of Spider
    name = 'splash_spider'
    # getting all the url + ip address + useragent pairs then request them

    def start_requests(self):
        # get the file path of the csv file that contains the pairs from the settings.py
        with open(self.settings["PROXY_CSV_FILE"], mode="r") as csv_file:
            # requests is a list of dictionaries like this -> {url: str, ua: str, ip: str}

            requests = process_csv(csv_file)
            for i, req in enumerate(requests):
                x = len(requests) - i
                # Return needed url with set delay of 3 seconds
                yield SplashRequest(url=req["url"], callback=self.parse, args={"wait": 3},
                    # Pair with user agent specified in csv file
                    headers={"User-Agent": req["ua"]},
                    # Sets splash_url to whatever the current proxy that goes with current URL  is instead of actual splash url
                    splash_url = req["ip"],
                    priority = x,
                    meta={'priority': x}  # <- check here!!
                    )

    # Scraping function that will scrape URLs for specified information
    def parse(self, response):
       # parse for first spider


class LoginSpider(BaseSpider):
   name = 'login_spider'
   my_urls  = ['https://www.starcitygames.com/myaccount/']
   def start_requests(self):
       for url in self.my_urls:
           yield Request(url, meta={'proxy': 'http://199.89.192.97::8050'})

   def parse(self, response):
       # parse for second spider
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...