ModuleNotFoundError: No module named 'bs4

有些话、适合烂在心里 提交于 2020-06-23 16:29:25

问题


When I try to import BeautifulSoup like this

from bs4 import BeautifulSoup

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 !

edit(My code)

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)

回答1:


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

Ie.

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

Should change that to:

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

OR

You can keep:

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

but then you need to have the import as:

import bs4

FULL CODE: OPTION 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)

FULL CODE: OPTION 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)



回答2:


Do a pip install bs4 and that will solve your error. If you have different versions of Python installed, try with pip2 or pip3 as per your requirement.

pip2 install bs4 # for Python2
pip3 install bs4 # for Python3



回答3:


If you're using PyCharm, close and restart PyCharm. Then hover mouse cursor over bs4, until the red bulb shows up. Use the first intention action - "Install beautifulsoup", then PyCharm will take care of the problem from there.

I had the same issue, and the intention action won't work until I reboot PyCharm.



来源:https://stackoverflow.com/questions/54201681/modulenotfounderror-no-module-named-bs4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!