To install both libraries, run python -m pip install requests beautifulsoup4. Requests downloads a web page, and Beautiful Soup (imported as bs4) reads the HTML so you can pull out titles, links and text.
The install command
python -m pip install requests beautifulsoup4
On Windows, py -m pip install requests beautifulsoup4 does the same. The Requests installation page gives the single-package form as python -m pip install requests. The python -m pip form matters: it makes sure pip belongs to the same interpreter that will run your script, which avoids most "installed but not found" problems covered below.
Spell the second name exactly: beautifulsoup4, with the digit. The Beautiful Soup documentation says that the package named BeautifulSoup is not what you want, because it is the old Beautiful Soup 3. As of the PyPI page for beautifulsoup4, the latest release is 4.15.0 (June 7, 2026), and it requires Python 3.7 or later.
Check that it worked
Save this as fetch_test.py and run python fetch_test.py:
import requests
from bs4 import BeautifulSoup
response = requests.get("https://example.com", timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
print(soup.title.get_text())
for link in soup.find_all("a"):
print(link.get("href"))
Example output (the page content can change):
Example Domain
https://iana.org/domains/example
If this prints a title and a link, both packages work. Here is what each line does:
requests.get(...)downloads the page and returns aResponseobject.raise_for_status()raisesrequests.exceptions.HTTPErrorfor a 4xx or 5xx status code, so a failed download does not get parsed as if it were a page.BeautifulSoup(response.text, "html.parser")builds a tree from the HTML text.soup.titleis the first<title>tag,get_text()returns the readable text inside it, andfind_all("a")returns every<a>tag.link.get("href")reads thehrefattribute. The Beautiful Soup documentation uses this same call in its link-listing loop.
Always pass a timeout
The Requests quickstart says that nearly all production code should use the timeout parameter in nearly all requests, and that without it your program can hang indefinitely. If no timeout is given, requests do not time out. The quickstart also clarifies that the timeout is not a limit on the whole download: an exception is raised if no bytes have been received on the underlying socket for that many seconds. A slow server that keeps sending data will not trigger it.
All exceptions that Requests raises on purpose inherit from requests.exceptions.RequestException, so one except clause can catch network problems (ConnectionError), timeouts (Timeout), too many redirects (TooManyRedirects) and bad status codes (HTTPError):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
except requests.exceptions.RequestException as error:
print(f"Request failed: {error}")
raise SystemExit(1)
Find the data you want
Three Beautiful Soup calls cover most pages. The first two below come straight from the documentation's examples.
soup.find(id="link3") # first tag with id="link3"
soup.select('a[href$="tillie"]') # CSS selector: links whose href ends in "tillie"
soup.find_all("a") # every link
find returns one tag (or None if nothing matches), while find_all and select return a list. Check for None before you call .get_text() on the result of find, or you will get an AttributeError on pages that lack the element.
To send data to a site rather than read from it, see Python requests POST JSON: json= vs data=, Headers and Errors.
Which parser to use
The second argument to BeautifulSoup() names the parser. Per the documentation:
| Parser | Install | Notes from the documentation |
|---|---|---|
html.parser | Built into Python | Nothing extra to install. Decent speed. |
lxml | pip install lxml | Very fast. Needs an external C dependency. Recommended for speed. |
html5lib | pip install html5lib | Pure Python. Very lenient: parses pages the way a browser does. Very slow. |
Start with html.parser. Switch to lxml if you parse many pages and speed matters, or to html5lib if a badly broken page parses into the wrong tree. Requesting a parser that is not installed raises an exception, so install it first with pip in the same environment.
Text, bytes and encoding
Requests guesses the encoding from the HTTP headers and uses that guess when you read response.text. You can check it with response.encoding and change it. The raw bytes are in response.content. If you already know a document's encoding, the Beautiful Soup documentation says you can pass it to the constructor as from_encoding to avoid detection mistakes. When accented letters come out garbled, the encoding is the first thing to check.
Keep the install repeatable
Put both names in a requirements.txt, one per line, and install with python -m pip install -r requirements.txt. The file format and its pitfalls are covered in pip install requirements.txt: How It Works and How to Fix Errors. To see what you have installed, run python -m pip show requests beautifulsoup4.
If it still fails
ModuleNotFoundError: No module named 'bs4'. The package is not installed in the Python that runs your script. People often typepip install bs4orimport beautifulsoup4by mistake. Installbeautifulsoup4, importbs4, and run both steps with the same interpreter.ModuleNotFoundError: No module named 'requests'. Same cause. Inside a virtual environment, activate it before installing and before running the script.error: externally-managed-environment. Your system Python blocks global installs. Create a virtual environment and install there. See Fix pip "error: externally-managed-environment" (PEP 668).- A file named
requests.pyorbs4.pyin your project. Python searches the script's folder first, so such a file shadows the real package. Rename it. - The element is in your browser but not in
soup. Requests only gets the HTML the server sends. If the page fills itself in with JavaScript, the data is not inresponse.text. Look in your browser's network tab for the API call the page makes, and request that instead. - A 403 or 429 status. The server is refusing or rate-limiting you. Check the site's terms and robots.txt, slow down, and read the HTTP 429 guide. Custom headers are passed as a dictionary to the
headersparameter, as the Requests quickstart shows withheaders={'user-agent': 'my-app/0.0.1'}.
Frequently asked questions
- Why is the package called beautifulsoup4 but imported as bs4?
- The distribution name on PyPI is beautifulsoup4, while the module you import is bs4. The Beautiful Soup documentation shows the import as from bs4 import BeautifulSoup. The older package named BeautifulSoup is Beautiful Soup 3, which the documentation says is not what you want.
- Do I need lxml or html5lib as well?
- No. Beautiful Soup works with the html.parser parser that is built into Python. The documentation lists lxml (very fast, install with pip install lxml) and html5lib (very lenient but very slow, install with pip install html5lib) as optional alternatives.
- Can I install requests and beautifulsoup4 with one pip command?
- Yes. pip accepts several package names in one command: python -m pip install requests beautifulsoup4. Both are installed into the environment that the python command belongs to.
- Does requests run JavaScript on a page?
- No. Requests downloads the response body the server sends, which you read as r.text or r.content. Content that a page adds later with JavaScript is not in that body, so Beautiful Soup cannot find it.