2014-02-24 40 views
0

我试图直接在我的python脚本中导入BeautifulSoup库,我无法安装它,因为我在Synology DS213 +中使用它,所以我试图做到这一点:如何在Python脚本中导入.py

from BeautifulSoup import BeautifulSoup 
import urllib, urllib2 

opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=0)) 
opener.addheaders = [('User-agent', 'Mozilla/5.0')] 

ins = open("str.txt", "r") 
array = [] 
for line in ins: 
    array.append(line.strip()) 
ins.close() 

for riga in array: 
    print riga 
    html = opener.open("http://www.mysite.com/?s="+riga) 
    soup = BeautifulSoup(html) 
    soup.find_all('a') 
    for link in soup.find_all('a'): 
     print link.get('href') 

,但我收到此错误:

Traceback (most recent call last): 
    File "myscript.py", line 17, in <module> 
    soup.find_all('a') 
TypeError: 'NoneType' object is not callable 

我不明白为什么,我把BeautifulSoup.py在myscript.py目录,我输入这样:

from BeautifulSoup import BeautifulSoup 

有什么不对?

+0

您使用的是什么版本的BeautifulSoup?通常模块名称是bs4,并且您导入它类似于“从bs4导入BeautifulSoup” –

+0

“TypeError”是* not *'ImportError'。如果这是你所有的,那么你已经*已经成功导入了'BeautifulSoup'。无论您的安装是否中断,都是另一个问题 – jfs

+0

我使用beautifulsoup 3 – Piero

回答

0
  1. 创建了一个名叫NeXT BeautifulSoupmyscript.py文件夹。

  2. 重命名文件BeautifulSoup.py__init__.py并将其放入BeautifulSoup文件夹中。