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

  • Home
  • SEARCH
  • 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 8489003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:42:18+00:00 2026-06-10T21:42:18+00:00

I’ve written a spider in Scrapy which is basically doing fine and does exactly

  • 0

I’ve written a spider in Scrapy which is basically doing fine and does exactly what it is supposed to do.
The problem is I need to make small change to it and I have tried several approaches without success (e.g. modifying the InitSpider). Here is what the script is supposed to do now:

  • crawl the start url http://www.example.de/index/search?method=simple
  • now proceed to the url http://www.example.de/index/search?filter=homepage
  • start the crawling from here with the pattern defined in the rules

So basically all that needs to be changed is to call one URL in between. I would rather not rewrite the whole thing with a BaseSpider, so I hoped that someone has an idea on how to achieve this 🙂

If you need any additional infos, please let me know. Below you can find the current script.

#!/usr/bin/python
# -*- coding: utf-8 -*-

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from example.items import ExampleItem
from scrapy.contrib.loader.processor import TakeFirst
import re
import urllib

take_first = TakeFirst()

class ExampleSpider(CrawlSpider):
    name = "example"
    allowed_domains = ["example.de"]

    start_url = "http://www.example.de/index/search?method=simple"
    start_urls = [start_url]

    rules = (
        # http://www.example.de/index/search?page=2
        # http://www.example.de/index/search?page=1&tab=direct
        Rule(SgmlLinkExtractor(allow=('\/index\/search\?page=\d*$', )), callback='parse_item', follow=True),
        Rule(SgmlLinkExtractor(allow=('\/index\/search\?page=\d*&tab=direct', )), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        hxs = HtmlXPathSelector(response)

        # fetch all company entries
        companies = hxs.select("//ul[contains(@class, 'directresults')]/li[contains(@id, 'entry')]")
        items = []

        for company in companies:
            item = ExampleItem()
            item['name'] = take_first(company.select(".//span[@class='fn']/text()").extract())
            item['address'] = company.select(".//p[@class='data track']/text()").extract()
            item['website'] = take_first(company.select(".//p[@class='customurl track']/a/@href").extract())

            # we try to fetch the number directly from the page (only works for premium entries)
            item['telephone'] = take_first(company.select(".//p[@class='numericdata track']/a/text()").extract())

            if not item['telephone']:
              # if we cannot fetch the number it has been encoded on the client and hidden in the rel=""
              item['telephone'] = take_first(company.select(".//p[@class='numericdata track']/a/@rel").extract())

            items.append(item)
        return items

Edit

Here is my attempt with the InitSpider: https://gist.github.com/150b30eaa97e0518673a
I got that idea from here: Crawling with an authenticated session in Scrapy

As you can see, it still inherits from CrawlSpider, but I made some changes to the core Scrapy files (not my favourite approach). I let the CrawlSpider inherit from InitSpider instead of BaseSpider (source).

This works so far, but the spider just stops after the first page instead of picking up all the other ones.

Also, this approach seems to be absolutely unnecessary to me 🙂

  • 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-10T21:42:19+00:00Added an answer on June 10, 2026 at 9:42 pm

    Ok, I found the solution myself and it is actually much simpler than I initially thought 🙂

    Here is the simplified script:

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    from scrapy.spider import BaseSpider
    from scrapy.http import Request
    from scrapy import log
    from scrapy.selector import HtmlXPathSelector
    from example.items import ExampleItem
    from scrapy.contrib.loader.processor import TakeFirst
    import re
    import urllib
    
    take_first = TakeFirst()
    
    class ExampleSpider(BaseSpider):
        name = "ExampleNew"
        allowed_domains = ["www.example.de"]
    
        start_page = "http://www.example.de/index/search?method=simple"
        direct_page = "http://www.example.de/index/search?page=1&tab=direct"
        filter_page = "http://www.example.de/index/search?filter=homepage"
    
        def start_requests(self):
            """This function is called before crawling starts."""
            return [Request(url=self.start_page, callback=self.request_direct_tab)]
    
        def request_direct_tab(self, response):
            return [Request(url=self.direct_page, callback=self.request_filter)]
    
        def request_filter(self, response):
            return [Request(url=self.filter_page, callback=self.parse_item)]
    
        def parse_item(self, response):
            hxs = HtmlXPathSelector(response)
    
            # fetch the items you need and yield them like this:
            # yield item
    
            # fetch the next pages to scrape
            for url in hxs.select("//div[@class='limiter']/a/@href").extract():
                absolute_url = "http://www.example.de" + url             
                yield Request(absolute_url, callback=self.parse_item)
    

    As you can see I’m now using a BaseSpider and just generating the new Requests myself at the end. And at the beginning I simply walk through all the different requests that need to be made before the crawling can start.

    I hope this is helpful for someone 🙂 If you have questions, I’ll gladly answer them.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.