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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:39:32+00:00 2026-06-12T18:39:32+00:00

I am trying to get the final redirected URL in scrapy. For example, if

  • 0

I am trying to get the final redirected URL in scrapy. For example, if an anchor tag has a specific format:

<a href="http://www.example.com/index.php" class="FOO_X_Y_Z" />

Then I need to get the URL that URL redirects to (if it does, if its 200 then OK). For example, I get the appropriate anchor tags like this:

def parse (self, response)  
    hxs     = HtmlXPathSelector (response);
    anchors = hxs.select("//a[@class='FOO_X_Y_Z']/@href");

    // Lets assume anchor contains the actual link (http://...)
    for anchor in anchors:
        final_url = get_final_url (anchor);   // << I would need something like this

        // Save final_url

So if I visited http://www.example.com/index.php and that would send me through 10 redirects and finally it would stop at http://www.example.com/final.php – this is what I would need get_final_url() to return.

I thought of hacking my way to a solution but am asking here to see if scrapy has one already provided?

  • 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-12T18:39:33+00:00Added an answer on June 12, 2026 at 6:39 pm

    Again, assuming anchor contains an actual URL, I went and accomplished it with urllib2:

    def parse (self, response)  
        hxs     = HtmlXPathSelector (response);
        anchors = hxs.select("//a[@class='FOO_X_Y_Z']/@href");
    
        // Lets assume anchor contains the actual link (http://...)
        for anchor in anchors:
            final_url = urllib2.open(anchor, None, 1).geturl()
    
            // Save final_url
    

    urllib2.open() returns a file-like object with two additional methods, one of them being geturl() which returns the final URL (after all redirects have been followed). Its not part of Scrapy, but it works.

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

Sidebar

Related Questions

I am trying to get http://www.emacswiki.org/emacs/PrettyGreek working for emacs 21.3.1 on Windows. I have
I'm trying to get a real equivalent for Java's public static final in Scala
I am trying to get the url of the page I just fetched -
I am trying to redirect from this: www.mysite.com/product.html?item=example to: www.mysite.com/example I have this rewrite
I am trying to get the timestamp. This works. final Calendar cal = new
I am trying to get the final model using backward elimination with R but
I am trying to get resteasy working on JBoss AS6 Final (SEAM 2 app),
I am trying to use display:table, table-row to get a final output as below.
Code: Jsoup.connect(http://www.warriorforum.com/).followRedirects(true).get(); Log cat error: 10-14 11:58:08.593: ERROR/MY(24780): TRY1: download failed java.io.IOException: Too many
So i am trying get 2 div-containers which both should contain centered text (Both

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.