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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:05:24+00:00 2026-06-09T18:05:24+00:00

This is a crawler that prints all the URLs available on a given link.

  • 0

This is a crawler that prints all the URLs available on a given link.

#!C:/Python27/python.exe -u
import urllib
import cgi,cgitb
cgitb.enable()

print "Content-Type: text/html\n\n"

def get_page(url):
    try:
        return urllib.urlopen(url).read()
except:
    return ""

def get_next_target(page):
    start_link = page.find('<a href=')
    if start_link == -1: 
        return None, 0
    start_quote = page.find('"', start_link)
    end_quote = page.find('"', start_quote + 1)
    url = page[start_quote + 1:end_quote]
    return url, end_quote

def get_all_links(page):
    links = []
    while True:
        url, endpos = get_next_target(page)
        if url:
            links.append(url)
            page = page[endpos:]
        else:
            break
    return links

def union(a, b):
    for e in b:
        if e not in a:
            a.append(e)

def add_page_to_index(index, url, content):
    words = content.split()
    for word in words:
        add_to_index(index, word, url)

def add_to_index(index, keyword, url):
    if keyword in index:
        index[keyword].append(url)
    else:
        index[keyword] = [url]

def lookup(index, keyword):
    if keyword in index:
        return index[keyword]
    else:
        return None

def crawl_web(seed): # returns index, graph of inlinks
    tocrawl = [seed]
    crawled = []
    graph = {}  # <url>, [list of pages it links to]
    index = {} 
    while tocrawl: 
        page = tocrawl.pop()
        if page not in crawled:
            content = get_page(page)
            add_page_to_index(index, page, content)
            outlinks = get_all_links(content)
            graph[page] = outlinks
            union(tocrawl, outlinks)
            crawled.append(page)
    return index, graph

index, graph = crawl_web('http://www.bing.com/results.asp?q=fulcrum')

print graph
print """
<html>
<body>
Animesh Pandey
</body>
</html>
"""
print "<br>"
print graph
print "<br>"
print index
print "<br>"
print tocrawl
print "<br>"
print seed

This python file runs fine on an online interpreter! At least it gives some result ….
but when running on a browser it always gives a timeout!!
I am using Apache 2.2.11 and Python 2.7.3.

Please tell me what should I try to do ???

  • 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-09T18:05:25+00:00Added an answer on June 9, 2026 at 6:05 pm

    This is the correct version of the code.

    #!C:/Python27/python.exe -u
    import urllib
    import cgi,cgitb
    cgitb.enable()
    
    print "Content-Type: text/html\n\n"
    
    def get_page(url):
        try:
            return urllib.urlopen(url).read()
    except:
        return ""
    
    def get_next_target(page):
        start_link = page.find('<a href=')
        if start_link == -1: 
            return None, 0
        start_quote = page.find('"', start_link)
        end_quote = page.find('"', start_quote + 1)
        url = page[start_quote + 1:end_quote]
        return url, end_quote
    
    def get_all_links(page):
        links = []
        while True:
            url, endpos = get_next_target(page)
            if url:
                links.append(url)
                page = page[endpos:]
            else:
                break
        return links
    
    def union(a, b):
        for e in b:
            if e not in a:
                a.append(e)
    
    def add_page_to_index(index, url, content):
        words = content.split()
        for word in words:
            add_to_index(index, word, url)
    
    def add_to_index(index, keyword, url):
        if keyword in index:
            index[keyword].append(url)
        else:
            index[keyword] = [url]
    
    def lookup(index, keyword):
        if keyword in index:
            return index[keyword]
        else:
            return None
    
    def crawl_web(seed): # returns index, graph of inlinks
        tocrawl = [seed]
        crawled = []
        graph = {}  # <url>, [list of pages it links to]
        index = {} 
        while tocrawl: 
            page = tocrawl.pop()
            if page not in crawled:
                content = get_page(page)
                add_page_to_index(index, page, content)
                outlinks = get_all_links(content)
                graph[page] = outlinks
                union(tocrawl, outlinks)
                crawled.append(page)
        return index, graph
    
    theurl = "http://dl.dropbox.com/u/86814352/ani.html"
    index, graph = crawl_web(theurl)
    
    print "<br>"
    print graph
    print "<br>"
    

    This prints the urls on that static web page but this code is not suitable for sites with many links!

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

Sidebar

Related Questions

Below is a link crawler that gets the urls of a page in a
I have a crawler that for HTTPs switches over to Wininet. This usually works
I have a simple web crawler that starts at root (given url) downloads the
First of, I'm doing this for a web crawler (aka spider aka worm...) Given
I'm working on a web crawler that grabs data from sites all over the
I'm writing a basic crawler that simply caches pages with PHP. All it does
Im new to python and in my code below: I have a crawler that
I am building a web application crawler that's meant not only to find all
I have the following code that extracts urls from a given page using jsoup.
I am currently building this web crawler to fetch all the links of 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.