BeautifulSoup - TypeError: 'module' object is not callable

1.4k views Asked by At

Can anyone explain to me why am I getting this error message? I have the exact same code in another project but after creating a new venv and install requests, bs4 and html.parser I get this message. I don't understand, at all.

import requests
import bs4 as BeautifulSoup
import html.parser

source = requests.get('https://docs.python.org/3/library/html.parser.html')
soup = BeautifulSoup(source.text, 'html.parser')

print(soup)
1

There are 1 answers

0
Jordan On

It looks like you just need to change your bs4 import. Try this:


import requests
from bs4 import BeautifulSoup
import html.parser

source = requests.get('https://docs.python.org/3/library/html.parser.html')
soup = BeautifulSoup(source.text, 'html.parser')

print(soup)

To explain what was wrong, basically you were importing the entire bs4 module in as BeautifulSoup, when you actually wanted to import the BeautifulSoup class from the bs4 module.