首页 > 解决方案 > 从 bs4 导入 BeautifulSoup ModuleNotFoundError:没有名为“bs4”的模块

问题描述

我正在做一个网络抓取项目,但我不明白为什么我的程序拒绝import bs4

我已经解决了几个小时的问题,尝试设置路径,确保安装了轮子,是的,我使用pip3了,我的pythonpip都更新了,大写似乎与其他人的进口相匹配......有人可以帮忙吗?代码摘录如下:

from bs4 import BeautifulSoup

import requests

horse_List = []

horse_List_Soup = []

BASE_URL = 'https://www.horseracingnation.com/horse/' #create a base url to build from

HEADERS = {'User-Agent': 'Mozilla/5.0'} #user-agent added so that webpage doesn't reject request

def get_Horse_Soup():

     for num in range(len(horse_List)):

        incorrect = True
        while(incorrect):
            response = requests.get(BASE_URL + horse_List[num]) #GET request method with specified url

            if not response.status_code == 200: #catches errors if request is not ok (code 200)
                if response.status.code == 404:
                    raise Exception("Horse not found. Try searching another name.")
                    name = input("Input a new name for rachorse #" + num + ": ")
                    name.replace(" ", "_")
                    horse_List[num] = name
                else:
                    raise Exception("Web request failed. Status Code: " + str(response.status_code))
            else:
                incorrect = False
                horse_List_Soup[num] = BeautifulSoup(response.content, "lxml") 

def main():
   get_Horse_Soup()

main()

追溯这里:

回溯(最后一次调用):文件“C:\Users\Kisra\Desktop\DataViz_Project\KO_HorseSoup.py”,第 1 行,从 bs4 导入 BeautifulSoup ModuleNotFoundError:没有名为“bs4”的模块

标签: pythonbeautifulsoup

解决方案


确保您运行代码的 python 内核与您 pip 安装的 bs4 相同。如果您有 32 位 python 内核 pip 包,则不适用于您的 64 内核,反之亦然


推荐阅读