-3

I'm terribly sorry if this is unacceptable or answered elsewhere, but I've spent the last hour and a half looking for information on it, and have come up with nothing I can use. I'm brand spanking new to Python and have been given an assignment to pull an IP from a site. I'm able to get my program to read the site, but I'm simply unable to figure out what to do next. Every answer that remotely comes close to what I want is beyond my programming capabilities to the point that I just don't understand it, and tutorials don't actually get at the specific problem I'm having. Again, if there's any document or text I can read instead of wasting time, please send me that way.

import urllib.request
site = urllib.request.urlopen("http://homer.wcitac.org/~sec290/hwk2/")
print (site.read())
IP = site[10]
print (IP)

I know, it's simplistic, but I've only been doing this for a little while. As far as I can tell, it should print back the 10th (9th on the page) character so I have a starting point, so I can then use a colon to find the characters I want, but it's giving me "TypeError: 'HTTPResponse' object does not support indexing", and I've no idea what that means.

Jeff C
  • 3

2 Answers2

1

You have the site variable pointed to the return value of urllib.request.urlopen. In the next line, you call site.read(), which returns a string. In short, site is not referencing a string; it's referencing a response object which can be used to get the string content.

Since you already know site.read() returns a string, why not capture that as a variable and use it?

content = site.read()
print(content)
bedwyr
  • 5,774
  • 4
  • 31
  • 49
  • That worked, thank you. Would you happen to have a very basic resource on how to use the find function in python as well? – Jeff C Oct 06 '14 at 18:37
  • Do you mean this? https://docs.python.org/2/library/stdtypes.html#str.find – bedwyr Oct 06 '14 at 19:04
0

Instead of printing the results of site.read(), store it in a variable, which will be a string containing all the text of the page, which you can then do with whatever you need to.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101