【问题标题】:Python-How to resolve TypeErrorPython-如何解决 TypeError
【发布时间】:2014-02-20 09:21:39
【问题描述】:
 import urllib, urllib2
 from bs4 import BeautifulSoup, Comment
 url='http://www.amazon.in/product-reviews/B00EJBA7HC/ref=cm_cr_pr_top_link_1?ie=UTF8&pageNumber=1&showViewpoints=0&sortBy=bySubmissionDateDescending'
 content = urllib2.urlopen(url).read()
 soup = BeautifulSoup(content, "html.parser")
 fooId = soup.find('input',name='ASIN',type='hidden') #Find the proper tag
 value = fooId['value']
 print value

我需要此代码从给定的 URL 打印产品的 ASIN ID。

相反,我收到以下错误:

TypeError: find() got multiple values for keyword argument 'name'

请帮忙。

【问题讨论】:

    标签: python python-2.7 beautifulsoup screen-scraping


    【解决方案1】:

    之所以会出现这种情况,是因为您的 soup.find 函数签名错误。没有第一个位置参数。函数签名如下所示:

    def find(self, name=None, attrs={}, recursive=True, text=None, **kwargs)
    

    所以“输入”被分配给第一个关键字参数(在本例中为名称)。所以现在您有 2 个值分配给关键字参数“名称”。

    您尝试做的正确语法可能是这样的:

    fooId = soup.find(name='input', attrs={'name': 'ASIN', 'type': 'hidden'})
    

    这表示,使用 attrs 中列出的属性找到您正在解析的 HTML 中的所有 <input>

    【讨论】:

    • 非常感谢!我得到了所需的输出!
    猜你喜欢
    • 2011-09-10
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2019-07-10
    • 1970-01-01
    • 2012-05-26
    • 2017-10-14
    相关资源
    最近更新 更多