我是通过pip命令下载的beautifulsoup4,但是在使用过程中显示‘No model named bs4’
#导入urlopen函数
from urllib.request import urlopen
#导入beautifulsoup4
from bs4 import beautifulsoup as bf
#请求获取HTML
html = urlopen('https://www.baidu.com')
#用bs解析html
obj = bf(html.read(),'html.parser')
#从head,title里提取标签
title = obj.head.title
#打印标题
print(title)
结果显示:
Traceback (most recent call last):
File "D:\python\baidu.py", line 4, in
from bs4 import beautifulsoup as bf
ModuleNotFoundError: No module named 'bs4'
开始我在网上搜寻后以为是安装路径不对,但是后来发现是和python同一路径,但多次重启程序和电脑后还是如上显示。
请问如何不再报错。
模块导错了,是这个
from bs4 import BeautifulSoup
有可能是您在下载bs4库后对python进行了版本更新。
此时,更新后的python不会继承使用前一个版本时额外下载的包。
此时,运行cmd或终端,再次下载bs4即可。
直接在当前执行环境再装一次:
pip install beautifulsoup4
你是不是有多个python环境?
或者你是创建项目时选择的是虚拟环境??