我已经使用easy_install安装了BeautifulSoup并尝试运行以下脚本
from BeautifulSoup import BeautifulSoup
import re
doc = ['<html><head><title>Page title</title></head>',
'<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
'<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
'</html>']
soup = BeautifulSoup(''.join(doc))
print soup.prettify()
但不确定为什么会发生这种情况
Traceback (most recent call last):
File "C:\Python27\reading and writing xml file from web1.py", line 49, in <module>
from BeautifulSoup import BeautifulSoup
ImportError: No module named BeautifulSoup
你能帮忙吗?
感谢
答案 0 :(得分:198)
试试这个from bs4 import BeautifulSoup
这可能是Beautiful Soup,第4版和beta版的问题。我刚在主页上看到了这个。
答案 1 :(得分:19)
在Ubuntu 14.04上我从apt-get安装它并且工作正常:
sudo apt-get install python-beautifulsoup
然后就这样做:
from BeautifulSoup import BeautifulSoup
答案 2 :(得分:8)
试试这个,我这样工作。获取任何标签数据只需替换&#34; a&#34;用你想要的标签。
from bs4 import BeautifulSoup as bs
import urllib
url="http://currentaffairs.gktoday.in/month/current-affairs-january-2015"
soup = bs(urllib.urlopen(url))
for link in soup.findAll('a'):
print link.string
答案 3 :(得分:3)
您可以导入bs4而不是BeautifulSoup。 由于bs4是内置模块,因此无需额外安装。
from bs4 import BeautifulSoup
import re
doc = ['<html><head><title>Page title</title></head>',
'<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
'<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
'</html>']
soup = BeautifulSoup(''.join(doc))
print soup.prettify()
如果要使用请求模块请求。
请求正在使用urllib
,requests
个模块。
但我个人建议使用requests
模块而不是urllib
模块安装使用:
$ pip install requests
以下是使用请求模块的方法:
import requests as rq
res = rq.get('http://www.example.com')
print(res.content)
print(res.status_code)
答案 4 :(得分:1)
首先安装靓汤版4。 在终端窗口中写命令:
pip install beautifulsoup4
然后导入BeutifulSoup库
答案 5 :(得分:0)
如果你有两个版本的python,也许我的情况可以帮助你
这是我的情况
1→ mac osx
2→我有两个版本的python,(1)系统默认版本2.7(2)手动安装版本3.6
3→我用sudo pip install beautifulsoup4
4-&GT;我使用python3 /XXX/XX/XX.py
所以这种情况3和4是关键部分,我已经安装了带有“pip”的beautifulsoup4但是这个模块是为python verison 2.7安装的,我用“python3”运行python文件。所以你应该为python 3.6安装beautifulsoup4;
使用sudo pip3 install beautifulsoup4
可以安装python 3.6的模块
答案 6 :(得分:0)
我在Windows 10上遇到同样的问题。
我像在Windows命令窗口(cmd)上那样建议安装它:
C:\Users\NAMEOFUSER\AppData\Local\Programs\Python\beautifulsoup4-4.8.2\setup.py install
BeautifulSoup像这样安装在我的python目录中:
C:\Users\NAMEOFUSE\AppData\Local\Programs\Python\Python38\Lib\site-packages\beautifulsoup4-4.8.2-py3.8.egg
在将bs4和EGG-INFO文件夹手动处理到site-packages文件夹后,所有内容都开始工作,例如:
from bs4 import BeautifulSoup
html = """
<html>
<body>
<p> Ich bin ein Absatz!</p>
</body>
</html>
"""
print(html)
soup = BeautifulSoup(html, 'html.parser')
print(soup.find_all("p"))
答案 7 :(得分:0)
如果以这种方式安装(如果没有,则以这种方式安装):
pip install beautifulsoup4
,如果您使用了此代码(如果没有使用,请使用此代码):
from bs4 import BeautifulSoup
如果您使用Windows系统,请检查是否有模块,可能会在模块中保存其他路径