Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:33:26+00:00 2026-06-12T08:33:26+00:00

I’ve managed to code a very simple crawler with Scrapy, with these given constraints:

  • 0

I’ve managed to code a very simple crawler with Scrapy, with these given constraints:

  • Store all link info (e.g.: anchor text, page title), hence the 2 callbacks
  • Use CrawlSpider to take advantage of rules, hence no BaseSpider

It runs well, except it doesn’t implement rules if I add a callback to the first request!

Here is my code: (works but not properly, with a live example)

from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from scrapySpider.items import SPage
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

class TestSpider4(CrawlSpider):
    name = "spiderSO"
    allowed_domains = ["cumulodata.com"]
    start_urls = ["http://www.cumulodata.com"]
    extractor = SgmlLinkExtractor()

    def parse_start_url(self, response):
        #3
        print('----------manual call of',response)
        self.parse_links(response)
        print('----------manual call done')
        # 1 return Request(self.start_urls[0]) # does not call parse_links(example.com)
        # 2 return Request(self.start_urls[0],callback = self.parse_links) # does not call parse_links(example.com)

    rules = (
        Rule(extractor,callback='parse_links',follow=True),
        )

    def parse_links(self, response):
        hxs = HtmlXPathSelector(response)
        print('----------- manual parsing links of',response.url)
        links = hxs.select('//a')
        for link in links:
                title = link.select('@title')
                url = link.select('@href').extract()[0]
                meta={'title':title,}
                yield Request(url, callback = self.parse_page,meta=meta)

    def parse_page(self, response):
        print('----------- parsing page: ',response.url)
        hxs = HtmlXPathSelector(response)
        item=SPage()
        item['url'] = str(response.request.url)
        item['title']=response.meta['title']
        item['h1']=hxs.select('//h1/text()').extract()
        yield item

I’ve tried solving this issue in 3 ways:

  • 1: To return a Request with the start url – rules are not executed
  • 2: Same as above, but with a callback to parse_links – Same issue
  • 3: Call parse_links after scraping the start url, by implementing parse_start_url, function does not get called

Here are the logs:

----------manual call of <200 http://www.cumulodata.com>)

----------manual call done

#No '----------- manual parsing links', so `parse_links` is never called!

Versions

  • Python 2.7.2
  • Scrapy 0.14.4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:33:27+00:00Added an answer on June 12, 2026 at 8:33 am

    Here’s a scraper that works perfectly:

    from scrapy.contrib.spiders import CrawlSpider,Rule
    from scrapy.selector import HtmlXPathSelector
    from scrapy.http import Request
    from scrapySpider.items import SPage
    from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
    
    class TestSpider4(CrawlSpider):
        name = "spiderSO"
        allowed_domains = ["cumulodata.com"]
        start_urls = ["http://www.cumulodata.com/"]
    
        extractor = SgmlLinkExtractor()
    
        rules = (
            Rule(extractor,callback='parse_links',follow=True),
            )
    
        def parse_start_url(self, response):
            list(self.parse_links(response))
    
        def parse_links(self, response):
            hxs = HtmlXPathSelector(response)
            links = hxs.select('//a')
            for link in links:
                title = ''.join(link.select('./@title').extract())
                url = ''.join(link.select('./@href').extract())
                meta={'title':title,}
                cleaned_url = "%s/?1" % url if not '/' in url.partition('//')[2] else "%s?1" % url
                yield Request(cleaned_url, callback = self.parse_page, meta=meta,)
    
        def parse_page(self, response):
            hxs = HtmlXPathSelector(response)
            item=SPage()
            item['url'] = response.url
            item['title']=response.meta['title']
            item['h1']=hxs.select('//h1/text()').extract()
            return item
    

    Changes:

    1. Implemented parse_start_url – Unfortunately, when you specify a callback for the first request, rules are not executed. This is inbuilt into Scrapy, and we can only manage this with a workaround. So we do a list(self.parse_links(response)) inside this function. Why the list()? Because parse_links is a generator, and generators are lazy. So we need to explicitly call it fully.

    2. cleaned_url = "%s/?1" % url if not '/' in url.partition('//')[2] else "%s?1" % url – There are a couple of things going on here:

      a. We’re adding ‘/?1’ to the end of the URL – Since parse_links returns duplicate URLs, Scrapy filters them out. An easier way to avoid that is to pass dont_filter=True to Request(). However, all your pages are interlinked (back to index from pageAA, etc.) and a dont_filter here results in too many duplicate requests & items.

      b. if not '/' in url.partition('//')[2] – Again, this is because of the linking in your website. One of the internal links is to ‘www.cumulodata.com’ and another to ‘www.cumulodata.com/’. Since we’re explicitly adding a mechanism to allow duplicates, this was resulting in one extra item. Since we needed perfect, I implemented this hack.

    3. title = ''.join(link.select('./@title').extract()) – You don’t want to return the node, but the data. Also: ”.join(list) is better than list[0] in case of an empty list.

    Congrats on creating a test website which posed a curious problem – Duplicates are both necessary as well as unwanted!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.