我试图在Python上使用bs4创建一个spider,我已经使用pip和easy_install安装了bs4,但是一旦我使用PyCharm运行程序,就会出现一个错误:
Traceback (most recent call last):
File "C:/PyCharm Project/bs4.py", line 3, in <module>
from bs4 import BeautifulSoup
File "C:\PyCharm Project\bs4.py", line 3, in <module>
from bs4 import BeautifulSoup
ImportError: cannot import name 'BeautifulSoup'
Process finished with exit code 1
但在命令提示符下,不会出现此错误:
C:\WINDOWS\system32>python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>>
>>> html_doc = """
... <html><head><title>The Dormouse's story</title></head>
... <body>
... <p class="title"><b>The Dormouse's story</b></p>
...
... <p class="story">Once upon a time there were three little sisters; and their names were
... <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
... <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
... <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
... and they lived at the bottom of a well.</p>
...
... <p class="story">...</p>
... """
>>>
>>> # create a bs object using a HTML page
... soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')
>>> soup.title
<title>The Dormouse's story</title>
>>> soup.title.string
"The Dormouse's story"
>>>
很有可能pycharm被配置为使用python2.7。。。您可以在“文件”>;“设置”>;“项目设置”>;“解释程序”下更改为3.5
或者
在文件顶部键入
import bs4
。。。当它用红色下划线时,将光标移到它下面。。。按alt+enter,然后选择“安装包bs4”您用
from bs4 import BeautifulSoup
调用了脚本C:/PyCharm Project/bs4.py"
,实际上您试图从自己的脚本而不是bs4
库导入BeautifulSoup
,因此需要重命名它并删除目录中的任何.pyc
文件。您应该重新安装它,或者将解释器更改为python 3.5或在文件顶部键入import bs4
相关问题 更多 >
编程相关推荐