python3.5:无法隐式地将字节转换为字符串

时间:2016-02-16 14:06:13

标签: python python-3.x beautifulsoup

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

1 个答案:

答案 0 :(得分:2)

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

如果您将其添加到string,python会自动尝试将u转换为string,但会引发此TypeError,因为它已编码。

您有两种选择:要么不首先对该文本进行编码(仅适用于u = u.get_text()),要么之后对其进行解码,即先使用u.decode('utf-8'),然后您就能够工作了用字符串。