I did this spider that checks the stock of references list (around 3500 references).
Now the spider takes around 37 seconds to scrape 400 references. The CPU is around 5%, the network card (1Gbps) around 18%. My internet connection is 300Mbps symmetric and it's only connected to this computer.
Any idea to improve performance? Is this a good performance? Maybe ISP router is a bottleneck?
import scrapy
from scrapy.crawler import CrawlerProcess
class Spider(scrapy.Spider):
name = "Spider"
start_urls = ['URLS']
def __init__(self, references=None, *args, **kwargs):
super(ktmSpider, self).__init__(*args, **kwargs)
def parse(self, response):
return scrapy.FormRequest.from_response(
response,
formdata={'UserName': 'username', 'Password': 'password'},
callback=self.after_login
)
def after_login(self, response):
ref = references.pop()
yield scrapy.Request(url="url" + ref, callback=self.parse_stock)
def parse_stock(self, response):
self.f.write(response.selector.xpath('//*[@id="priceDetails"]/form/div[2]/text()').extract_first() + ',')
self.f.write(response.selector.xpath('//*[@id="priceDetails"]/form/div[8]/div[1]/span/span[2]/text()').extract_first() + ',')
self.f.write(response.selector.xpath('//*[@id="priceDetails"]/form/div[8]/div[1]/span/span[1]/i/@style').extract_first() + '\n')
while len(references) > 0:
ref = references.pop()
yield scrapy.Request(url="url" + ref, callback=self.parse_stock)
f = open("references.txt")
references = f.read().splitlines()
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
'LOG_LEVEL': 'INFO',
'AUTOTHROTTLE_ENABLED': 'True',
'AUTOTHROTTLE_START_DELAY': '0.1',
'AUTOTHROTTLE_TARGET_CONCURRENCY': '100'
})
process.crawl(ktmSpider, references=references, stockFile="file.txt")
process.start()