gpt4 book ai didi

python3.5 :Can't convert bytes to string implicitly

转载 作者:行者123 更新时间:2023-12-01 04:06:54 24 4
gpt4 key购买 nike

for url_ in url_list:
print("Processing {}...\n".format(url_))
r_new = rq.get(url_)
soup = bsoup(r_new.text)
h = soup.findAll("div", {"class": "fk-review"})
for row in h:
reviewfile.write(str(pe) + ') \n')
b = row.findAll("div", {"class": "rating"})

for a in b:
c = a.get('style').strip()
c = ratings_string(c)

reviewfile.write('Ratings: ' + (c) + '\n')
local_rating = c
c = row.findAll("a", {"class": "load-user-widget"})

for s in c:
f = s.get_text().strip()

reviewfile.write("Name: " + f + '\n')
local_user = f

y = row.find_all("span", {"class": "review-text"})
for u in y:
u = u.get_text().encode('utf-8', errors='ignore').strip()
local_review = u

str1 = local_review.lower()

reviewfile.write('\n' + u + '\n \n')

pe += 1

我正在尝试检索文本文件中的产品评论,但它显示它可以隐式地将字节转换为字符串,请帮助我......提前致谢

错误是:

Traceback (most recent call last):
File "E:\Mtech projects\SET 2\flip.py", line 175, in <module>
reviewfile.write('\n' + u + '\n \n')
TypeError: Can't convert 'bytes' object to str implicitly

最佳答案

您的 u 对象已编码
这一行就做到了u = u.get_text().encode('utf-8',errors='ignore').strip()

如果将其添加到字符串,Python会自动尝试将u转换为字符串,但会引发此 TypeError 因为它是经过编码的。

您有两个选择:要么首先不对该文本进行编码(仅适用于 u = u.get_text()),或者稍后对其进行解码,即使用 u。首先解码('utf-8'),然后您就可以使用字符串了。

关于python3.5 :Can't convert bytes to string implicitly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com