AttributeError: 'HTTPResponse' object has no attribute 'replace'

8.3k views Asked by At

Hi i get the above error. Why does it pop up, what am I missing and how do I get around it ? Thanks

try:
    import urllib.request as urllib2
except ImportError:
    import urllib2

from html2text import html2text

sock = html2text(urllib2.urlopen('http://www.example.com')) 
htmlSource = sock.read()                            
sock.close()                                        
print (htmlSource)

Im running IDLE 3.4.3 on a Windows 7 OS.

3

There are 3 answers

1
Alex Ivanov On

I think I found a solution for Python 3.4. I just decoded source into UTF-8 and it worked.

#!/usr/bin/python

try:
    import urllib.request as urllib2
except ImportError:
    import urllib2

from html2text import html2text

source=urllib2.urlopen('http://www.example.com').read() 
s=html2text(source.decode("UTF-8"))

print (s)

Output

# Example Domain

This domain is established to be used for illustrative examples in documents.
You may use this domain in examples without prior coordination or asking for
permission.

[More information...](http://www.iana.org/domains/example)
1
alecxe On

html2text expects the HTML code passed in as a string - read the response:

source = urllib2.urlopen('http://www.example.com').read()
text = html2text(source)
print(text)

It prints:

# Example Domain

This domain is established to be used for illustrative examples in documents.
You may use this domain in examples without prior coordination or asking for
permission.

[More information...](http://www.iana.org/domains/example)
4
Alex Ivanov On

Replace is an attribute for strings and you have a fileobject

obj=urllib2.urlopen('http://www.example.com')
print obj

.

<addinfourl at 3066852812L whose fp = <socket._fileobject object at 0xb6d267ec>>

This one is OK.

#!/usr/bin/python

try:
    import urllib.request as urllib2
except ImportError:
    import urllib2

from html2text import html2text


source=urllib2.urlopen('http://www.example.com').read() 
s=html2text(source)

print s

Output

This domain is established to be used for illustrative examples in documents.
You may use this domain in examples without prior coordination or asking for
permission.

[More information...](http://www.iana.org/domains/example