Attributeerror: 'httpresponse' Object Has No Attribute 'replace'
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
Solution 1:
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)
Solution 2:
I think I found a solution for Python 3.4. I just decoded source into UTF-8 and it worked.
#!/usr/bin/pythontry:
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)
Solution 3:
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/pythontry:
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
Post a Comment for "Attributeerror: 'httpresponse' Object Has No Attribute 'replace'"