【问题标题】:Digikey part price python scriptDigikey 零件价格 python 脚本
【发布时间】:2013-07-22 12:41:57
【问题描述】:

我正在尝试编写一个脚本来搜索 digikey.com 以查找零件并返回每个零件的价格中断。但是我在打开网址时遇到了麻烦。我查看了其他类似的脚本,这就是我想出的,但我收到 BeautifulSoup 错误。我正在使用 Python 2.7 并运行 Ubuntu 13.04。

#!/usr/bin/python

# This script will find the page of a part and return the price 
# break information

import BeautifulSoup
import urllib2


# Create Url to read
Digikey_url = 'http://digikey.com/scripts/DkSearch/dksus.dll?Detail&name='
partNum = '458-1003-ND'
url=Digikey_url+partNum

# Create BeautifulSoup Object 
page = urllib2.urlopen(url)
soup = BeautifulSoup(response)


# Close Page
page.close()

这是我得到的错误:

Traceback (most recent call last):
  File "DigiKeyPrice.py", line 17, in <module>
    soup = BeautifulSoup(page)
TypeError: 'module' object is not callable

我也是 python 新手,但如果有任何帮助,我们将不胜感激。

谢谢

【问题讨论】:

    标签: python web screen-scraping


    【解决方案1】:

    替换:

    import BeautifulSoup
    

    与:

    from BeautifulSoup import BeautifulSoup
    

    另外,response变量没有定义,替换:

    soup = BeautifulSoup(response)
    

    与:

    soup = BeautifulSoup(page)
    

    【讨论】:

    • ahhh... 我发现我忘记更改该变量了,我试图摆脱其他一些示例。但我想如果我只是做了import BeautifulSoup 它会导入整个模块。我错了吗?
    • 是的,但是你需要来自BeautifulSoup 模块的BeautifulSoup 类。
    • 谢谢,这正是我的问题所在。我以为我已经读过我可以从 BeautifulSoup 导入所有内容,但我现在知道我的问题出在哪里,这就是为什么我收到错误的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多