Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
print leaves console blank
-
2107 characters ;-) Not 20,000 ;-)
-
I stupidly run this code. Straight after my iPad Pro started acting crazy. Is this a type of a attack at that url? If it's not I apologise.
-
It seems to be some kind of encoding issue. To be honest, I can't quite figure out why exactly this doesn't work, but you're making your life a little harder than it needs to be by using
httplib
directly.If you use
requests
instead, your code becomes much simpler, and it also happens to work:from HTMLParser import HTMLParser import requests class MyHTMLParser(HTMLParser): t='' def handle_data(self, data): if len(data) > 40: self.t = self.t + '\n' + data def get_text(self): return self.t class textfromweb: def get_weather_text(self): r = requests.get('http://prognoza.hr/prognoze_e.php?id=jadran_n', timeout=50) data = r.text parser = MyHTMLParser() parser.feed(data) self.text = parser.get_text() return self.text t = textfromweb() print t.get_weather_text()
-
-
@omz
.decode('latin1') fixes the problem too in the original code.
The text included several \xfc type umlats. -
@JonB Ah, right, I mixed up
encode
anddecode
when I tried that. Happens to me all the time... -
@omz the whole encode/decode is a mess that i can never get right. to_unicode and to_bytes would be so much more pythonic and clear!
-
@JonB @omz I can't do anything in that category at all… when I get encoding / decoding errors I usually just try to stick
x.encode('utf-8')
,unicode(x)
, andx.decode('utf-8')
wherever I can and hope it works 😛I'm really usually at a loss when it comes to making programs that work with non-ASCII characters. It took me several months before I could make wikipedia map work with Unicode characters. If either of you knows any good resources or guides on that, I'd love to hear.
-
@pythonista , as I said I apologise if wrong. Which appears I am. Just after I ran your code, my ipad starting going crazy. It could have been water on my screen or something else. Just bad timing.
Again sorry, i just hard to ask the hard way, I am not good enough to know if I was under attack or not. -
@omz @JonB @Webmaster4o If it helps, http://bit.ly/unipain is a good explanation of what Unicode and text encodings are, why they always go wrong, why Python 2 is weird and how to do Unicode properly in Python.
-
When I get encoding / decoding errors I usually just... switch to Python 3. ;-)
One thing to watch out for is that ALL HTMLParsers share a single
.t
.One piece of syntactic sugar could be to define a
HTMLParser.__str__()
method in place of theHTMLParser.get_text()
method...def __str__(self): return self.t # ... print(parser)
-
@dgelessus Your article describes my situation perfectly:
your program started belching UnicodeErrors. You kind of knew what to do with those, so you added an encode or a decode where the error was raised, but the UnicodeError happened somewhere else.