gpt4 book ai didi

python - 在函数中调用时未定义 BeautifulSoup

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:46 26 4
gpt4 key购买 nike

当我在函数内部调用 BeautifulSoup() 时,我的网络抓取工具抛出 NameError: name 'BeautifulSoup' is not Define ,但当我在函数外部调用它并将 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))

但是当我将 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')

最佳答案

我猜你犯了一些BeautifulSoup的拼写错误,它区分大小写。如果没有,请在代码中使用请求:

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')

关于python - 在函数中调用时未定义 BeautifulSoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52358022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com