如何在python脚本中导入.py

时间:2014-02-24 18:51:49

标签: python beautifulsoup

我正在尝试直接在我的python脚本中导入BeautifulSoup库,我无法安装它,因为我在我的Synology DS213 +中使用它,所以我试图这样做:

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

出了什么问题?

2 个答案:

答案 0 :(得分:1)

此处没有导入错误。您已成功导入BeautifulSoup

soup.find_all更改为soup.findAll以修复TypeError

.find_all()适用于beautifulsoup4。您已安装BeautifulSoup 3。此版本中没有.find_all()方法。

如果没有现有.something属性,则默认为soup.something will try to find <something> element in the htmlNone表示没有<find_all>元素。请参阅“将标记名称用作成员”部分。

答案 1 :(得分:0)

  1. BeautifulSoup文件旁边创建一个名为myscript.py的文件夹。

  2. 将文件BeautifulSoup.py重命名为__init__.py并将其放在BeautifulSoup文件夹中。