Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在函数中调用时未定义美化组_Python_Beautifulsoup - Fatal编程技术网

Python 在函数中调用时未定义美化组

Python 在函数中调用时未定义美化组,python,beautifulsoup,Python,Beautifulsoup,我的web scraper正在抛出NameError:当我在函数内部调用BeautifulSoup()时,名称“beautifulsou”未定义,但当我在函数外部调用它并将Soup作为参数传递时,它正常工作 以下是工作代码: from teams.models import * from bs4 import BeautifulSoup from django.conf import settings import requests, os, string soup = BeautifulSo

我的web scraper正在抛出
NameError:当我在函数内部调用BeautifulSoup()时,名称“beautifulsou”未定义
,但当我在函数外部调用它并将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')

首先将BeautifulSoup导入变量,然后使用它

    from bs4 import BeautifulSoup as yourVariable

它不是说
名称url没有定义吗
?因为它没有在您的第二个示例中定义。编辑以反映我的代码您真的从代码中复制了示例吗?如果没有,请检查拼写错误(例如,尝试区分大小写的搜索)请包含完整的错误消息。请不要只发布代码,添加说明以获取更多帮助