简体   繁体   English

Python UnicodeDecodeError

[英]Python UnicodeDecodeError

I am building a little website crawler and I've encountered some problems with it. 我正在构建一个小的网站搜寻器,但遇到了一些问题。 The first one would be Unicode characters in the url 第一个是url中的Unicode字符

Let's say I have the following url : http://putlocker.is/actor/Juan_Fern%C3%A1ndez 假设我有以下网址: http : //putlocker.is/actor/Juan_Fern%C3%A1ndez

My code is : 我的代码是:

        try:

             connection = urllib.urlopen(self.__link)
             get = connection.read().decode('utf8')


        except:
            if UnicodeDecodeError:
                 print("UnicodeDecodeError !!!")  

I'm talkink about the original link , not about the encoded one 我说的是原始链接,而不是编码的链接

Your way of error handling seems to be wrong. 您的错误处理方式似乎是错误的。 Expression under your if-statement UnicodeDecodeError will always be True. if语句UnicodeDecodeError下的表达式将始终为True。 You probably should change it to 您可能应该将其更改为

try:
    ...
except UnicodeDecodeError:
    #handle error

In your case any error is just swallowed so you don't even see what the actual error is. 在您的情况下,任何错误都被吞没了,因此您甚至看不到实际的错误是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM