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)