Page 2 of 14 FirstFirst 123412 ... LastLast
Results 11 to 20 of 138

Thread: [Beginner] Programming Challenge: 4

  1. #11
    Join Date
    Jun 2007
    Location
    England
    Beans
    760
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Beginner] Programming Challenge: 4

    my first attempt, html parsing coming soon... i hope ( see post 18 )
    Code:
    #!/usr/bin/env python
    
    # import modules
    import urllib2
    import sys
    
    class web_get:
    	def __init__(self):
                    # get desired web page
    		print "Welcome - web page downloader\n"
    		get_page = raw_input('Please enter web page to download: ')
                    # test if web page given
    		while get_page == "":
    			print "You must enter a web page to continue"
    			get_page = raw_input('Please enter web page to get: ')
    		print "Please wait..."
                    # open the web page
    		try:
    			web_page = urllib2.urlopen(get_page)
    		except:
    			print "\n--> An error ocurred, please try again (include the http://)"
    			sys.exit(1)
                    # write web page to file
    		for line in web_page:
    			open('web_page.xhtml', 'a').write(line)
    		open('web_page.xhtml').close()
    		print "Done"
                    sys.exit(0)
    if __name__ == '__main__':
    	try:
    		web_get()
    	except KeyboardInterrupt:
    		print "\n\nExiting..."
    		sys.exit(0)
    dje
    Last edited by dje; August 14th, 2008 at 08:52 PM.

  2. #12
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 4

    i would like to thank you for coming up with an abosultly burtal task that i am definetly going to try...any hints on what i could type into google to get help (like a name for what were doing)?

  3. #13
    Join Date
    May 2008
    Beans
    2

    Re: [Beginner] Programming Challenge: 4

    I started learning Python yesterday, and I created this in Windows in Notepad++, so I'm not sure if anything needs to added for Linux/Ubuntu compatability as I've only been running my programs through the Windows console. Anyway, here's my attempt:

    PHP Code:
    # Beginner Challenge 4 in Python
    # by greps5 8/14/08

    import urllib2

    a
    0# a = file-in fail/success flag, b = file-out flag

    # # # Read in webpage source # # #
    try:
        
    wbpg raw_input("\nEnter the url of the desired webpage: ")
        print 
    "\nLoading webpage source from %s ...\n" wbpg
        webpage 
    urllib2.urlopen(wbpg)
        
    source webpage.read()
        
    webpage.close()
        
    1
    except ValueError
    :
        print 
    "Sorry, %s is not a valid url" wbpg
    except IOError
    :
        print 
    "Error, unable to obtain source from %s" wbpg
    # # # End read-in # # #

    # # # Write source to file index.xhtml # # #
    if == 1:    
        print 
    "Saving webpage source to index.xhtml...\n"
        
    try:
            
    sourceOut open('index.xhtml''w')
            
    sourceOut.write(source)
            
    sourceOut.close()
            
    1
        except IOError
    :
            print 
    "Error, unable to save source\n"
    # # # End file-writing  # # #

    if == and ==1:
        print 
    "Source was successfully processed!\n" 
    I'll try to update it later to include the webpage <title>. I also think I'll give it a try in Java when I get a chance.

  4. #14
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [Beginner] Programming Challenge: 4

    Quote Originally Posted by jimi_hendrix View Post
    i would like to thank you for coming up with an abosultly burtal task that i am definetly going to try...any hints on what i could type into google to get help (like a name for what were doing)?
    For C#, the class you can use are: HTTPWebRequest and HTTPWebResponse (they are .NET, so you'll have to look around if they aren't part of the ECMA standard (which mono follows))

    Basically, you are downloading a web page, or doing http with a language.
    Last edited by LaRoza; August 14th, 2008 at 06:37 PM.

  5. #15
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 4

    LaRoza you have to be inside my computer to know i was going to do C#

  6. #16
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [Beginner] Programming Challenge: 4

    Quote Originally Posted by jimi_hendrix View Post
    laroza you have to be inside my computer to know i was going to do c#
    प्रतिरोध व्यर्थ है

  7. #17
    Join Date
    Feb 2007
    Location
    Edinburgh, Scotland
    Beans
    391

    Re: [Beginner] Programming Challenge: 4

    Quick attempt in Python. I have spent far too long attempting to learn Perl this week, which is obvious because of the mess of line 19.

    Code:
    import urllib2
    import sys
    
    #Try to get the address from args, else get it from a prompt
    try:
        address = sys.argv[1]
    except IndexError:
        address = str(raw_input("Address: "))
    
    #Attempt to connect to the website
    try:
        xhtml = urllib2.urlopen(address)
    except urllib2.HTTPError:
        print "Could not connect to %s and pull down data" % address
        sys.exit(1)
        
    #Read data from site and get title
    data = xhtml.read()
    title = data.split("<title>")[1].split("</title>")[0] #Crufty hack to get title
    
    if len(title) < 1:
        print "Couldnt get page title, using address instead"
        title = address #If somehow we didnt get the title, write the the address
    
    #Write data into the local file
    open(title + ".xhtml", 'w').write(data)
    print "%s -> %s.xhtml" % (address, title)
    Today we have 15 minutes of fame, tomorrow 15 minutes of anonymity.
    My Blog | Veza - Opensource TinyURL clone with sensible URLs

  8. #18
    Join Date
    Jun 2007
    Location
    England
    Beans
    760
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Beginner] Programming Challenge: 4

    here's my latest:
    Code:
    #!/usr/bin/env python
    
    import urllib2
    import sys
    import os
    
    class web_get:
    	def __init__(self):
    		print "Welcome - web page downloader\n"
    		# get desired web page
    		get_page = raw_input('Please enter web page to download: ')
    		# test if web page given
    		while get_page == "":
    			print "You must enter a web page to continue"
    			get_page = raw_input('Please enter web page to get: ')
    		print "Please wait..."
    		# open web page
    		try:
    			web_page = urllib2.urlopen(get_page)
    		except:
    			print "\n--> An error ocurred, please try again (include the http://)"
    			sys.exit(1)
    		# write web page to file
    		for line in web_page:
    			title_line = '<title>' in line
    			if title_line == True:
    				title = line[7:-9]
    			open('web_page.xhtml', 'a').write(line)
    		open('web_page.xhtml').close()
    		os.rename('web_page.xhtml', title + '.xhtml')
    		print "Done"
    		sys.exit(0)
    if __name__ == '__main__':
    	try:
    		web_get()
    	except KeyboardInterrupt:
    		print "\n\nExiting..."
    		sys.exit(0)
    Last edited by dje; August 14th, 2008 at 08:52 PM. Reason: added latest code

  9. #19
    Join Date
    Aug 2008
    Location
    Here?
    Beans
    13

    Re: [Beginner] Programming Challenge: 4

    My try:

    Python
    PHP Code:
    #!/usr/bin/env python
    #challenge 4
    import urllib2
    0
    while == 0:
        
    address raw_input("Enter a web address: ")
        try:
            
    page urllib2.urlopen(address)
            
    1
            
    print "Please wait..."
            
    break
        
    except:
            print 
    "couldn't find address, check for mistakes"
            
    0
    pageread 
    page.read()
    #get the title
    try:
        
    title pageread.split("<title>")[1]
        
    title title.split("</title>")[0]
    except:
        
    title "webpage"
    #name the new file
    out open(str(title) + ".xhtml""w")
    out.write(pageread)
    out.close()

    test raw_input("Done!"
    popcorn
    Last edited by cardboardtoast; August 14th, 2008 at 07:39 PM.

  10. #20
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 4

    Quote Originally Posted by LaRoza View Post
    प्रतिरोध व्यर्थ है
    can u translate that? and i expected something in borg not arabic

Page 2 of 14 FirstFirst 123412 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •