Volatil3
Volatil3

Reputation: 14998

Python: urllib2.urlopen retrns of type string

I am trying to fetch a URL content by making a request first and then using urlopen function but when I tried to close the stream it gave me type error. Upon investigation I found it's returning string type. Below is my code:

 req = urllib2.Request(url, '', HEADERS)
 html = urllib2.urlopen(req).read()
 print(type(html)) #retrns str

I want to close urllib stram. How do I do it?

Upvotes: 0

Views: 1900

Answers (1)

user824425
user824425

Reputation:

You're not closing the stream. You're closing the string returned by .read(), and closing a string doesn't make sense.

Try to store the result of urlopen(...) somewhere (perhaps in a variable called stream) before calling .read() on it, so that you can also .close() it after you're done..

Upvotes: 2

Related Questions