Skillversitas

Souping the Page

"Souping" a page means passing raw HTML or XML to the BeautifulSoup constructor and getting back a document tree you can navigate. The constructor takes two arguments: the text to parse and the parser to use.

From a String

from bs4 import BeautifulSoup

html = "<html><body><p class="x">Hi</p></body></html>"
soup = BeautifulSoup(html, "html.parser")
print(soup.p)              # <p class="x">Hi</p>
print(type(soup))          # <class 'bs4.BeautifulSoup'>

From a File

Open the file in binary mode so Beautiful Soup can detect the encoding itself:

with open("page.html", "rb") as f:
    soup = BeautifulSoup(f, "lxml")
print(soup.title.text if soup.title else "no title")

From an HTTP Response

Pass the response object or its text to the constructor. requests objects expose .text (decoded string) and .content (raw bytes):

import requests
from bs4 import BeautifulSoup

resp = requests.get("https://example.com", timeout=10)
soup = BeautifulSoup(resp.text, "html.parser")
print(soup.title.text)

Encoding Handling

If you parse a byte string, Beautiful Soup uses chardet-style detection and honors meta charset tags. If you pass a str, it assumes the encoding is already resolved. When text comes out garbled, decode explicitly:

text = raw_bytes.decode("utf-8")
soup = BeautifulSoup(text, "html.parser")

Summary

The BeautifulSoup constructor is the entry point: pass HTML or XML text (or a binary file handle) plus a parser name, and you get a tree. Soupe from strings, files opened in binary mode, or requests responses, and let the library handle encoding when you pass bytes.