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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:43:33+00:00 2026-06-03T09:43:33+00:00

I have a basic loop to look for links on a page i have

  • 0

I have a basic loop to look for links on a page i have retrieved with urllib2.urlopen, however i am trying to only follow internal links on a page..

Any ideas how to make my below loop get only links that are on the same domain?

for tag in soupan.findAll('a', attrs={'href': re.compile("^http://")}): 
                webpage = urllib2.urlopen(tag['href']).read()
                print 'Deep crawl ----> ' +str(tag['href'])
                try:
                    code-to-look-for-some-data...

                except Exception, e:
                    print e
  • 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-03T09:43:35+00:00Added an answer on June 3, 2026 at 9:43 am
    >>> import urllib
    >>> print urllib.splithost.__doc__
    splithost('//host[:port]/path') --> 'host[:port]', '/path'.
    

    if the host is the same or the host is empty(which is for relative paths) the url belongs to the same host.

    for tag in soupan.findAll('a', attrs={'href': re.compile("^http://")}):
    
                href = tag['href']
                protocol, url = urllib.splittype(href) # 'http://www.xxx.de/3/4/5' => ('http', '//www.xxx.de/3/4/5')
                host, path =  urllib.splithost(url)    # '//www.xxx.de/3/4/5' => ('www.xxx.de', '/3/4/5')
                if host.lower() != theHostToCrawl and host != '':
                    continue
    
                webpage = urllib2.urlopen(href).read()
    
                print 'Deep crawl ----> ' +str(tag['href'])
                try:
                    code-to-look-for-some-data...
    
                except:
                    import traceback
                    traceback.print_exc()
    

    because you do this

    'href': re.compile("^http://")
    

    no relative paths will be used.
    the are like

    <a href="/folder/file.htm"></a>
    

    maybe do not use re at all?

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

Sidebar

Related Questions

I have a For Each loop written in Visual Basic that's trying to modify
I'm learning assembly and I have a very basic loop here segment .data msg:
I'm writing a basic war-driving program. I have gotten it to loop the command
I have the basic code to rewrite a subdomain to another page. But how
If I have some basic game loop in a thread, like this: public void
Does a For Each loop in Visual Basic have an iteration count, or would
I'm trying to run a basic loop that will find a specific value in
I have been trying to declare variables inside a For loop for quite some
I have a basic Clojure script containing: (def test (future (loop [] (println Running)
I have a pretty basic understanding of the GUI thread and the message loop,

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.