Python 3 - TypeError: a bytes-like object is required, not 'str'

北城以北 提交于 2019-11-30 14:11:50

In python 3 strings are by default unicode. The b in b'true' means that the string is a byte string and not unicode. If you don't want that you can do:

 from urllib.request import urlopen
 #check text for curse words  
 def check_profanity():
    with urlopen("http://www.wdylike.appspot.com/?q=shit") as f:
        output = f.read().decode('utf-8')
        if output:
            if "true" in output:
                print("There is a profane word in the document")

check_profanity()

Using with will close the urlopen connection automatically.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!