[python] gold price tracker

Questions about programming languages and debugging
Post Reply
User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

[python] gold price tracker

Post by moudy »

hello every one, I have been working on this code for like a week, it works fine. It is supposed to get the source code of this website:

Code: Select all

http://www.goldalert.com/
and will search for the gold price, the time, and change from previous reading in percent.
this is the code, I commented every line becuase im a bit paranoid about comments, and i like every thing well commented (any feed back in enhancing the code is appreciated):

Code: Select all

import urllib2
import re
import time

while 1: 
#reading web page source, and making a list of lines (source lines)
        res = urllib2.urlopen("http://www.goldalert.com/")
        lines = res.readlines()

#getting the line that has the gold data
        for i in lines:
                if i.find('<div id="gp">') > -1:
                        g_l = lines[lines.index(i)]
        l_g_p = re.findall('[\d\.]{1,50}', g_l)
        gold_price = l_g_p[0]

#getting the line that has the change data
        for i in lines:
                if i.find('<div id="chg"') > -1:
                        c_l = lines[lines.index(i)]
                	

        l_e_c_n = re.findall('[\+\-][\d\.]{1,10}', c_l)
        change_number = l_e_c_n[0] 

        l_e_c_p = re.findall('[\+\-][\d\.]{1,10}[\%]', c_l)
        change_percent = l_e_c_p[0]

#getting the line that has the time data
        for i in lines:
                if i.find('<div id="tm">') > -1:
                        time_line = lines[lines.index(i)]
        time_p = re.sub('\t<div id="tm"> ', '',time_line)
        time = re.sub('</div>', '',time_p)

#managing data
        final = gold_price+'   '+change_number+'   '+change_percent+'   '+time
        msn_1 = gold_price+'   '+'[c=4]'+change_number+'   '+change_percent+'[/c]'+'   '+time #negative = red
        msn_2 = gold_price+'   '+'[c=3]'+change_number+'   '+change_percent+'[/c]'+'   '+time #positive = green
        msn_0 = gold_price+'   '+'[c=1]'+change_number+'   '+change_percent+'[/c]'+'   '+time #positive = green


#opening output file
        fileHandle2 = open ( 'output.txt', 'a' )

#writing data to output file
        if change_number < 0:
                fileHandle2.write(msn_1)
        elif change_number == 0:
                fileHandle2.write(msn_0)
        elif change_number > 0:
                fileHandle2.write(msn_2)

#closing output file
        fileHandle2.close()

time.sleep(60)


I used the urllib2 library to fetch the HTML code, is there any other way to get the HTML source ? I'm gonna add more stuff to the script. so for now I would like to get any feedback if possible.
mahmoud_shihab@hotmail.com

User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

Re: [python] gold price tracker

Post by moudy »

and this is the code pasted to the suck-o code bin:
http://www.code.suck-o.com/42345
mahmoud_shihab@hotmail.com

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Re: [python] gold price tracker

Post by l0ngb1t »

nice one...
you may not use to track the gold price but learning how to read a web page and manage a text is great =D>
gonna do it with java once i get some free time
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

Post Reply