Traceback (most recent call last):
File "bs4.py", line 1, in <module>
import bs4
File "/home/mhadi/Desktop/bs4test/bs4.py", line 5, in <module>
soup = bs4.BeautifulSoup(site,'lxml')
AttributeError: module 'bs4' has no attribute 'BeautifulSoup'
代码:
import bs4
import urllib.request
site = urllib.request.urlopen('http://127.0.0.1:8000').read()
soup = bs4.BeautifulSoup(site,'lxml')
#for i in site:
# print(site[i])
print(soup)
问题是您的文件名bs4.py
.现在,如果您编写import
语句,Python 将首先查找具有该名称的本地文件。因此,它假定您的import bs4
引用您自己的文件。因此,您的文件将旨在导入自身,但它显然不包含所需的模块。
快速解决方法是重命名文件。例如进入bs4tests.py
.然后你可以使用import bs4
.
或者,您可以尝试删除本地路径,例如:
import sys # import sys package
old_path = sys.path[:] # make a copy of the old paths
sys.path.pop(0) # remove the first one (usually the local)
import bs4 # import the package
sys.path = old_path # restore the import path