Reputation: 39
I am trying to get a certain line in Python but I am stuck.
Here is my code:
import urllib2
response = urllib2.urlopen('http://pythonforbeginners.com/')
info = response.info()
html = response.read()
# do something
response.close() # best practice to close the file
if "x" in str(info):
print str(info)
raw_input()
I am trying to just get a line to display the type of server.
Upvotes: 0
Views: 235
Reputation: 568
I guess what your line is meaning a line in http header. You can get the http header by following code:
import urllib2
response = urllib2.urlopen('http://pythonforbeginners.com/')
info = response.info()
html = response.read()
# do something
response.close() # best practice to close the file
# this is how to process the http header
for key in info :
print key, info[key]
raw_input()
or you can convert the info to string and split by \r\n(http header is separate by \r\n) as follow
for line in str(info).split("\r\n") :
print line, "=="
Upvotes: 1
Reputation: 33029
Do you really need to treat info
as a string? If not, this should work pretty well:
for h in info.headers:
if h.startswith('Server'):
print h
Upvotes: 1