类型错误:无法将 str 连接到字节(我可以将其视为错误)



所有这些都是我的代码部分,

scr1 = driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/ul/div/li[%s]' % i)
driver.execute_script("arguments[0].scrollIntoView();", scr1)
sleep(1)
text = scr1.text
list = text.encode('utf-8').split()
dirname = os.path.dirname(os.path.abspath(__file__))
csvfilename = os.path.join(dirname, account + "-" + page + ".txt")
file_exists = os.path.isfile(csvfilename)
f = open(csvfilename, 'a')
f.write(list[0] + 'rbn')
f.close()
print('{};{}'.format(i, list[0]))
if i == (count - 1):
print(x)

所以目前当我运行这部分代码时,我收到以下错误,

f.write(list[0] + 'rn')
TypeError: can't concat str to bytes

谁能帮助我像我应该改变什么? 谢谢你,我非常感谢。

这是因为 encode(( 函数的返回类型是一个字节数组。例如:

'ábc'.encode('UTF-8')

生成 b'\xc3\xa1bc'

尝试:

f.write(list[0])
f.write('rbn')

相关内容

最新更新