我无法将Unicode转换为纯字符串

时间:2019-10-28 22:01:18

标签: python-3.x beautifulsoup

我被困triyng只能将一个单词从unicode转换为纯字符串。我在寻找答案,但没人能帮助我解决这个简单的问题。

我已经尝试了以下链接: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch03s18.html

Convert a Unicode string to a string in Python (containing extra symbols)

How to convert unicode string into normal text in python

from bs4 import BeautifulSoup

r = requests.get('https://www.mpgo.mp.br/coliseu/concursos/inscricoes_abertas')
soup = BeautifulSoup(r.content, 'html.parser')

table = soup.find('table', attrs={'class':'grid'})

text = table.get_text()
text_str = text[0:7]
text_str = text_str.encode('utf-8')

test_str = 'Nenhum'
test_str = test_str.encode('utf-8')

if text_str == test_str:
    print('Ok they are equal')
else:
    print(id(text_str))
    print(id(test_str))
    print(type(test_str))
    print(type(test_str))
    print(test_str)
    print(test_str)```

My spected result is: text_str being equal test_str

1 个答案:

答案 0 :(得分:0)

欢迎来到SO。您的调试输出中有错字。最后4个值都是te s t_str,而不是一些te x t_str。

然后您会注意到您的读入变量包含:

'\nNenhum'

因此,如果您将切片更改为:text_str = text [ 1 :7]或相应地设置测试字符串:

test_str = '\nNenhum'

有效。骇客入侵...

相关问题