ModuleNotFoundError:没有名为"bs4"的模块 [英] ModuleNotFoundError: No module named 'bs4

查看:172
本文介绍了ModuleNotFoundError:没有名为"bs4"的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试这样导入BeautifulSoup

When I try to import BeautifulSoup like this

from bs4 import BeautifulSoup

当我运行代码时,出现此错误消息. ModuleNotFoundError: No module named 'bs4

And when I run my code, I've this error message. ModuleNotFoundError: No module named 'bs4

如果有人知道如何解决此问题,那就太好了!

If someone know how to resolve this problem, it's will be great !

编辑(我的代码)

import os
import csv
import requests
import bs4

requete = requests.get("https://url")
page = requete.content
soup = BeautifulSoup(page)

h1 = soup.find("h1", {"class": "page_title"})
print(h1.string)

推荐答案

您a)尚未安装BeautifulSoup或b)没有看到您的代码,只能猜测您的代码中有bs4:

You either a) have not installed BeautifulSoup or b) without seeing your code, can only guess you have bs4 in your code:

即.

soup = bs4.BeautifulSoup(html, 'html.parser')

应将其更改为:

soup = BeautifulSoup(html, 'html.parser')

OR

您可以保留:

soup = bs4.BeautifulSoup(html, 'html.parser')

但随后您需要将导入内容设置为:

but then you need to have the import as:

import bs4

完整代码:选项1

import os
import csv
import requests
import bs4     #<-----------------------NOTICE

requete = requests.get("https://url")
page = requete.content
soup = bs4.BeautifulSoup(page)  #<-----------------------NOTICE

h1 = soup.find("h1", {"class": "page_title"})
print(h1.string)

完整代码:选项2

import os
import csv
import requests
from bs4 import BeautifulSoup   #<-----------------------NOTICE

requete = requests.get("https://url")
page = requete.content
soup = BeautifulSoup(page)  #<-----------------------NOTICE

h1 = soup.find("h1", {"class": "page_title"})
print(h1.string)

这篇关于ModuleNotFoundError:没有名为"bs4"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆