find函数的参数
我正在使用Beautiful Soup(一个Python库)。我有一个这样的隐藏输入对象:
<input type="hidden" name="form_build_id" id="form-531f740522f8c290ead9b88f3da026d2" value="form-531f740522f8c290ead9b88f3da026d2" />
我需要获取它的id和value。
这是我的代码:
mainPageData = cookieOpener.open('http://page.com').read()
soupHandler = BeautifulSoup(mainPageData)
areaId = soupHandler.find('input', name='form_build_id', type='hidden')
TypeError: find() got multiple values for keyword argument 'name'
我尝试修改了代码:
print soupHandler.find(name='form_build_id', type='hidden')
None
这有什么问题吗?
1 个回答
39
试试使用替代的attrs
关键字:
areaId = soupHandler.find('input', attrs={'name':'form_build_id', 'type':'hidden'})
你不能使用一个叫做 name 的关键字参数,因为 Beautiful Soup 的搜索方法已经定义了一个 name 参数。你也不能使用像 for 这样的 Python 保留字作为关键字参数。
Beautiful Soup 提供了一个特殊的参数叫做 attrs,你可以在这些情况下使用。attrs 是一个字典,和关键字参数的作用是一样的。