Anders Juengst
Anders Juengst

Reputation: 49

BeautifulSoup not defined when called in function

My web scraper is throwing NameError: name 'BeautifulSoup' is not defined when I call BeautifulSoup() inside my function, but it works normally when I call it outside the function and pass the Soup as an argument.

Here is the working code:

from teams.models import *
from bs4 import BeautifulSoup
from django.conf import settings
import requests, os, string

soup = BeautifulSoup(open(os.path.join(settings.BASE_DIR, 'revolver.html')), 'html.parser')

def scrapeTeamPage(soup):
    teamInfo = soup.find('div', 'profile_info')
...
print(scrapeTeamPage(soup))

But when I move the BeautifulSoup call inside my function, I get the error.

from teams.models import *
from bs4 import BeautifulSoup
from django.conf import settings
import requests, os, string

def scrapeTeamPage(url):
    soup = BeautifulSoup(open(os.path.join(settings.BASE_DIR, url)), 'html.parser')
    teamInfo = soup.find('div', 'profile_info')

Upvotes: 5

Views: 20451

Answers (2)

王淳龙
王淳龙

Reputation: 47

Import BeautifulSoup first into a variable and then use it.

    from bs4 import BeautifulSoup as yourVariable

Upvotes: 0

Sushant Kathuria
Sushant Kathuria

Reputation: 133

I guess you are doing some spelling mistake of BeautifulSoup, its case sensitive. if not, use requests in your code as:

from teams.models import *
from bs4 import BeautifulSoup
from django.conf import settings
import requests, os, string

def scrapeTeamPage(url):
    res = requests.get(url)
    soup = BeautifulSoup(res.content, 'html.parser')
    teamInfo = soup.find('div', 'profile_info')

Upvotes: 4

Related Questions