Search code examples
pythonattributesbyteprettify

Python bs4 'bytes' object has no attribute 'prettify'


I've recently tried to pull data from a website but it seems that I get an error which I can't solve on my own. So I started looking around to see if any other people had my exact same error and I found 3 to be precise, I tried all 3 solutions and a couple more but nothin seems to work.

Here is my code:

import bs4
from time import sleep
import requests
import os
import sys
import io

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,'cp437','backslashreplace')

f = open('output.txt', 'w')
f.close()
project_name = 'reddit'
url = 'https://www.reddit.com/'
html = requests.get(url)
soup = bs4.BeautifulSoup(html.text, 'html.parser').encode('utf-8')

print(soup.prettify())

As you can see I tried sys.stdout = io.TextIOWrapper(sys.stdout.buffer,'cp437','backslashreplace') which was suggested by someone else on another thread but unfortunately it did not work in my case.

If anyone around know the solution it would be very much appreciated, thanks in advance.

Naomi,


Solution

  • Remove the .encode('utf-8'):

    soup = bs4.BeautifulSoup(html.text, 'html.parser')