UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode

后端 未结 1 963
北恋
北恋 2021-01-16 03:45

I know many people encountered this error before but I couldn\'t find the solution to my problem.

I have a URL that I want to normalize:

url = u\"ht         


        
相关标签:
1条回答
  • 2021-01-16 04:49

    urllib.quote() does not properly parse Unicode. To get around this, you can call the .encode() method on the url when reading it (or on the variable you read from the database). So run url = url.encode('utf-8'). With this you get:

    import urllib
    import urlparse
    from urlparse import urlsplit
    
    url = u"http://www.dgzfp.de/Dienste/Fachbeitr%C3%A4ge.aspx?EntryId=267&Page=5"
    url = url.encode('utf-8')
    scheme, host_port, path, query, fragment = urlsplit(url)
    path = urllib.unquote(path)
    path = urllib.quote(path,safe="%/")
    

    and then your output for the path variable will be:

    >>> path
    '/Dienste/Fachbeitr%C3%A4ge.aspx'
    

    Does this work?

    0 讨论(0)
提交回复
热议问题