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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:57:26+00:00 2026-06-09T00:57:26+00:00

Im new to python and in my code below: I have a crawler that

  • 0

Im new to python and in my code below: I have a crawler that recurses on new discovered links. After recursing on the root link, it seems like the program is stop after printing a couple of links, this should go on for a while, but its not. I am catching and printing exceptions but the program terminates successful, so im not really sure why its stopping.

from urllib import urlopen
from bs4 import BeautifulSoup

def crawl(url, seen):
    try:
    if any(url in s for s in seen):
       return 0
    html = urlopen(url).read()

    soup = BeautifulSoup(html)
    for tag in soup.findAll('a', href=True):
        str = tag['href']
        if 'http' in str:
        print tag['href']
        seen.append(str)
        print "--------------"
        crawl(str, seen)
    except Exception, e:
      print e
      return 0

def main ():
    print "$ = " , crawl("http://news.google.ca", [])


if __name__ == "__main__":
    main()
  • 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-09T00:57:28+00:00Added an answer on June 9, 2026 at 12:57 am
    try:
        if any(url in s for s in seen):
           return 0
    

    and then

    seen.append(str)
    print "--------------"
    crawl(str, seen)
    

    You append str to seen, and then call crawl with str and seen as arguments. Obviously your code exits. You have designed it in such a way.

    A better way would be to crawl one page, add all the links found to a list that is to be crawled, and then keep crawling all the links in that list.

    In simpler terms, instead of going for depth first crawling, you should do breadth first crawling.

    Something like this should work.

    from urllib import urlopen
    from bs4 import BeautifulSoup
    
    def crawl(url, seen, to_crawl):
        html = urlopen(url).read()
        soup = BeautifulSoup(html)
        seen.append(url)
        for tag in soup.findAll('a', href=True):
            str = tag['href']
            if 'http' in str:
                if url not in seen and url not in to_crawl:
                    to_crawl.append(str)
                    print tag['href']
                    print "--------------"
        crawl(to_crawl.pop(), seen, to_crawl)
    
    def main ():
        print "$ = " , crawl("http://news.google.ca", [], [])
    
    
    if __name__ == "__main__":
        main()
    

    Though you might wanna put a limit on the maximum depth or max number of urls it will crawl.

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

Sidebar

Related Questions

I have a piece of Python code that is supposed to open a new
I'm just learning about python. I'm fairly new. I have the following code that
I have been trying to debug the below python cgi code but doesn't seems
I have the code below in my Python script: def cmd_wui(argv, path_to_tx): Run a
New to programming. I wrote a Python code (with help from stackoverflow) to read
I am new to Python. The following code is causing an error when it
I'm fairly new to Python, so I'm trying my hand at some simple code.
I am new to coding python. I am hoping to modify this code to
New to Python, have a simple, situational question: Trying to use BeautifulSoup to parse
I'm very new to Python and multithreaded programming in general. Basically, I have a

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.