import urllib2
import urllib
from BeautifulSoup import BeautifulSoup # html
from BeautifulSoup import BeautifulStoneSoup # xml
import BeautifulSoup # everything
import re
f = o.open( 'http://www.google.com', p)
html = f.read()
f.close()
soup = BeautifulSoup(html)
收到错误,说汤的行= BeautifulSoup(html)说“模块”对象不可调用。
答案 0 :(得分:25)
您的import BeautifulSoup
使BeautifulSoup
引用模块,而不是from BeautifulSoup import BeautifulSoup
之后的类。如果您要导入整个模块,可能需要省略from ...
行或者之后重命名该类:
from BeautifulSoup import BeautifulSoup
Soup = BeautifulSoup
...
import BeautifulSoup
....
soup = Soup(html)
答案 1 :(得分:6)
@ Blair的答案有正确的倾向,但我的表现略有不同,即:
import BeautifulSoup
Soup = BeautifulSoup.BeautifulSoup
(推荐)或
import BeautifulSoup
from BeautifulSoup import BeautifulSoup as Soup
(也不错)。
答案 2 :(得分:-1)
安装BeautifulSoup4
sudo easy_install BeautifulSoup4
<强>建议强>
来自bs4 import BeautifulSoup