Dirty_Fox
Dirty_Fox

Reputation: 1751

How to convert an urlopen into a string in python

Most certainly my question is not asked properly . Anyway, I am looking for a way to convert some data I ve extracted from the web to a list or a string (string would be better tho).

At the moment, this is what I wrote :

import urllib as web

message = "http://hopey.netfonds.no/posdump.php?date=20150209&paper=AAPL.O&csv_format=txt"   
data = web.urlopen(message)

data has a very weird type I ve never seen before (yes yes still new to that python thing). So any help would be very helpful .

Thanks in advance.

Upvotes: 3

Views: 5120

Answers (1)

Matei
Matei

Reputation: 1803

You can use the metod .read()

data = web.urlopen(message)
str_data = data.read()

This will return the html on the page. You can use dir(web.urlopen(message)) to see all the methods available for that object. You can use dir() to see the methods available for anything in python.

To sum up the answer, on that object you crated you can call the method .read() ( like data.read()) or you can use .readline( like data.readline(), this will read just a line from the file, you can use this method to read just a line when you need it).When you read from that object you will get a string back.

If you do data.info() you will get something like this :

<httplib.HTTPMessage instance at 0x7fceeb2ca638>

You can read more about this here .

Upvotes: 4

Related Questions