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 4590898
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:19:03+00:00 2026-05-21T22:19:03+00:00

In the Scrapy docs , there is the following example to illustrate how to

  • 0

In the Scrapy docs, there is the following example to illustrate how to use an authenticated session in Scrapy:

class LoginSpider(BaseSpider):
    name = 'example.com'
    start_urls = ['http://www.example.com/users/login.php']

    def parse(self, response):
        return [FormRequest.from_response(response,
                    formdata={'username': 'john', 'password': 'secret'},
                    callback=self.after_login)]

    def after_login(self, response):
        # check login succeed before going on
        if "authentication failed" in response.body:
            self.log("Login failed", level=log.ERROR)
            return

        # continue scraping with authenticated session...

I’ve got that working, and it’s fine. But my question is: What do you have to do to continue scraping with authenticated session, as they say in the last line’s comment?

  • 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-05-21T22:19:03+00:00Added an answer on May 21, 2026 at 10:19 pm

    In the code above, the FormRequest that is being used to authenticate has the after_login function set as its callback. This means that the after_login function will be called and passed the page that the login attempt got as a response.

    It is then checking that you are successfully logged in by searching the page for a specific string, in this case "authentication failed". If it finds it, the spider ends.

    Now, once the spider has got this far, it knows that it has successfully authenticated, and you can start spawning new requests and/or scrape data. So, in this case:

    from scrapy.selector import HtmlXPathSelector
    from scrapy.http import Request
    
    # ...
    
    def after_login(self, response):
        # check login succeed before going on
        if "authentication failed" in response.body:
            self.log("Login failed", level=log.ERROR)
            return
        # We've successfully authenticated, let's have some fun!
        else:
            return Request(url="http://www.example.com/tastypage/",
                   callback=self.parse_tastypage)
    
    def parse_tastypage(self, response):
        hxs = HtmlXPathSelector(response)
        yum = hxs.select('//img')
    
        # etc.
    

    If you look here, there’s an example of a spider that authenticates before scraping.

    In this case, it handles things in the parse function (the default callback of any request).

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        if hxs.select("//form[@id='UsernameLoginForm_LoginForm']"):
            return self.login(response)
        else:
            return self.get_section_links(response)
    

    So, whenever a request is made, the response is checked for the presence of the login form. If it is there, then we know that we need to login, so we call the relevant function, if it’s not present, we call the function that is responsible for scraping the data from the response.

    I hope this is clear, feel free to ask if you have any other questions!


    Edit:

    Okay, so you want to do more than just spawn a single request and scrape it. You want to follow links.

    To do that, all you need to do is scrape the relevant links from the page, and spawn requests using those URLs. For example:

    def parse_page(self, response):
        """ Scrape useful stuff from page, and spawn new requests
    
        """
        hxs = HtmlXPathSelector(response)
        images = hxs.select('//img')
        # .. do something with them
        links = hxs.select('//a/@href')
    
        # Yield a new request for each link we found
        for link in links:
            yield Request(url=link, callback=self.parse_page)
    

    As you can see, it spawns a new request for every URL on the page, and each one of those requests will call this same function with their response, so we have some recursive scraping going on.

    What I’ve written above is just an example. If you want to “crawl” pages, you should look into CrawlSpider rather than doing things manually.

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

Sidebar

Related Questions

This is the BaseSpider example from the Scrapy tutorial: from scrapy.spider import BaseSpider from
In the Python Scrapy framework there is a scrapy.bat file: @echo off setlocal %~dp0..\python
I'm trying to use Scrapy to scrape the U.S. government regulations website (www.regulations.gov). It's
I want to be able to use scrapy to crawl links on a sitemap.
I'm a newbie to python and scrapy and I'm following the dmoz tutorial. As
Need example in scrapy on how to get a link from one page, then
Please take a look at this spider example in Scrapy documentation. The explanation is:
By default Scrapy uses scrapy.cfg in the projects root. Is there a way to
I'm using scrapy to crawl multiple pages on a site. The variable start_urls is
Trying to install Scrapy on Mac OSX 10.6 using this guide : When running

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.