ModuleNotFoundError:没有名为“bs4”的模块,但已在Python 3.8.5上使用PIP3正确安装

2024-05-20 09:38:58 发布

您现在位置:Python中文网/ 问答频道 /正文

对不起,我知道这个问题有点提出来了,但我被难住了。以前工作的精细代码现在不再工作。我尝试过卸载和重新安装Python3.8.5,设置新的环境,将代码剥离回基本内容。我已经多次安装和卸载BS4和Beautifulsoup4

我的方法是pip3 install bs4

安装进行得很顺利,但是当我运行代码时

File "/Users/rupertdenton/Desktop/Coding/Anybody/tester.py", line 1, in <module>
    from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'

我对python版本进行了三次检查,它是3.8.5,并再次成功地重新安装:

Requirement already satisfied: soupsieve>1.2 in ./venv/lib/python3.8/site-packages (from beautifulsoup4->bs4) (2.0.1)
Installing collected packages: beautifulsoup4, bs4
Successfully installed beautifulsoup4-4.9.1 bs4-0.0.1

但代码仍然抛出相同的错误。这是大约一周前运行良好的缩减代码

from bs4 import BeautifulSoup

def scrapecafes(city, area):

    #url = 'https://www.broadsheet.com.au/melbourne/guides/best-cafes-thornbury' #go to the website
    url = f"https://www.broadsheet.com.au/{city}/guides/best-cafes-{area}"
    response = requests.get(url, timeout=5)

    soup_cafe_names = BeautifulSoup(response.content, "html.parser")
    type(soup_cafe_names)

    cafeNames = soup_cafe_names.findAll('h2', attrs={"class":"venue-title", }) #scrape the elements
    cafeNamesClean = [cafe.text.strip() for cafe in cafeNames]

Tags: 代码infromimporturlcitycafenames