ImportError: cannot import name BeautifulSoup

参考资料:
《ImportError: cannot import name BeautifulSoup(点这里就对了)》

问题

import bs4
from  bs4 import BeautifulSoup
file= open(r"C:\Users\lord\Desktop\b.html","rb")
html=file.read()
bs=BeautifulSoup(html,"html.parser")

print(bs.title)
输出结果:
Traceback (most recent call last):
  File "D:\PycharmProjects\pythonProject\douban\test1\bs4.py", line 6, in <module>
    import bs4
  File "D:\PycharmProjects\pythonProject\douban\test1\bs4.py", line 7, in <module>
    from bs4 import BeautifulSoup
ImportError: cannot import name 'BeautifulSoup' from partially initialized module 'bs4' (most likely due to a circular import) (D:\PycharmProjects\pythonProject\douban\test1\bs4.py)

解决办法:

重命名“bs4.py”文件,这里用“bs4”命名工程名,与模组“bs4.py”冲突了

import bs4
from bs4 import BeautifulSoup
file= open(r"C:\Users\lord\Desktop\b.html","rb")
html=file.read()
bs=BeautifulSoup(html,"html.parser")

print(bs.title)

ImportError: cannot import name BeautifulSoup_第1张图片

测试正常:

输出结果:

<title>百度一下,你就知道</title>

Process finished with exit code 0

你可能感兴趣的:(Python学习私人笔记,python,爬虫)