6

I'm having trouble with Scrapy. I need code that will scrap up to 1000 internal links per given url. My code works when run at command line, but the spider doesn't stop, only receives the message.

My code is as follows:

from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.item import Item, Field
from scrapy.contrib.closespider import CloseSpider

class MyItem(Item):
    url= Field()

class MySpider(CrawlSpider):
    name = 'testspider1'
    allowed_domains = ['angieslist.com']
    start_urls = ['http://www.angieslist.com']

    rules = (Rule(SgmlLinkExtractor(), callback='parse_url', follow=True), )

    def parse_url(self, response):
        item = MyItem()
        item['url'] = response.url

        scrape_count = self.crawler.stats.get_value('item_scraped_count')
        print scrape_count

        limit = 10

        if scrape_count == limit:
            raise CloseSpider('Limit Reached')

        return item
5
  • Are you sure it doesn't stop? I believe scrapy will contrinue to process already requests items after CloseSpider was recieved. I mean, it won't stop immediately, but no new items will be requested
    – Konstantin
    Jul 6, 2015 at 13:02
  • 1
    This could be interesting for you. Jul 6, 2015 at 13:05
  • You're right, it does stop. But, I'm planning to scale this process up and would like to remove any unnecessary processing. I was hoping that there would be some way to "stop on a dime." so to speak.
    – Josh Usre
    Jul 6, 2015 at 13:07
  • 1
    Also, why don't you use doc.scrapy.org/en/latest/topics/…
    – Konstantin
    Jul 6, 2015 at 13:18
  • Frank, I think you found what I need. I'm just trying to figure out how to implement / test it. Alik, I'll give it a go.
    – Josh Usre
    Jul 6, 2015 at 13:30

1 Answer 1

10

My problem was trying to apply close spider in the wrong place. It's a variable that needs to be set in the settings.py file. When I set it manually in there, or set it as a argument in the command line, it worked (Stopping within 10-20 of N for what it's worth).

settings.py:

BOT_NAME = 'internal_links'
SPIDER_MODULES = ['internal_links.spiders']
NEWSPIDER_MODULE = 'internal_links.spiders'
CLOSESPIDER_PAGECOUNT = 1000
ITEM_PIPELINES = ['internal_links.pipelines.CsvWriterPipeline']
# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'yo mama'
LOG_LEVEL = 'DEBUG'
3
  • I have implemented the same methodology, and as you say, it is stopping between 10-20 of the desired value. Do you know why this is? For completeness, I want my spider to stop instantly!
    – gtlambert
    Nov 8, 2016 at 16:17
  • From what I remember, the process is async and therefore not immediate when making request to the process. It's been a long time though and I'm working on memory, so this may not be the case anymore.
    – Josh Usre
    Nov 8, 2016 at 18:46
  • 10
    at least you crawled responsibly by identifying yourself in the user agent Mar 5, 2018 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.