小编And*_*gst的帖子

在函数中调用时未定义 BeautifulSoup

当我在函数内部调用 BeautifulSoup() 时,我的网络抓取工具会抛出NameError: name 'BeautifulSoup' is not defined异常,但当我在函数外部调用它并将 Soup 作为参数传递时,它会正常工作。

这是工作代码:

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))
Run Code Online (Sandbox Code Playgroud)

但是当我将 BeautifulSoup 调用移动到我的函数中时,我收到错误。

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')
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

beautifulsoup ×1

python ×1