seek()
Zutroi Zatatakowski
abou at cam.org
Sun Mar 31 10:49:06 EST 2002
Peter Hansen wrote:
> Well, that's the basic key to writing software: use components
> that already exist to build up a more sophisticated solution.
>
> In this case, if you have a method of finding a string in a string
> (which you have already found), and you have a method of getting
> a string from a file or webpage, you can clearly put the two together
> to solve your overall problem.
>
> Getting a string from the data in a file is rather simple, and if
> you want to get a string from a web page you should look at the
> urllib module.
>
> -Peter
This is what I have so far:
import urllib
import string
c = open('/home/corestats', 'w')
try:
f =
urllib.urlopen("http://www.coremud.org/cgi-bin/mycore.pl?stocks=1&font=Arial")
except IOError:
print '\nWebsite not reachable. Core 2651 must be down or check your
connection.'
k =
urllib.urlretrieve("http://www.coremud.org/cgi-bin/mycore.pl?stocks=1&font=Arial",
'corestats')
# Now I have the source in a file. No problems here.
# This finds the byte where 'Gold' starts. I know that Gold's value is
found 8 # bytes later.
longstr = c.read()
print string.find(longstr, 'Gold')
# Is it possible to have the output of string.find() converted to a
string, or even # better, to an int? If I can get the location of 'Gold'
into an int, it would be # easy to do:
c.seek(1469) # this is where 'Gold' is
c.read(4) # this gets Gold's value (in this case, 9.60)
c.close()
# But since the webpage is dynamic, it's never the same bytes so I can't
hardcode # the location of the different values I want.
# I'm not trying to get the string.find() output into an int...
--
Zutroi Zatatakowski
Commando PACU
http://pacu.cjb.net
More information about the Python-list
mailing list