python - 查找功能的参数

标签 python find beautifulsoup

我正在使用漂亮的汤(在 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

怎么了?

最佳答案

尝试使用 alternative attrs keyword :

areaId = soupHandler.find('input', attrs={'name':'form_build_id', 'type':'hidden'})

You can't use a keyword argument called name because the Beautiful Soup search methods already define a name argument. You also can't use a Python reserved word like for as a keyword argument.

Beautiful Soup provides a special argument called attrs which you can use in these situations. attrs is a dictionary that acts just like the keyword arguments.

关于python - 查找功能的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2877114/

相关文章:

python - 将每个表写入 csv,后跟标题

python - 用键盘中断关闭所有线程

linux - find -exec 中的命令替换

c++ - C++在队列中查找某些元素

python - 当我尝试通过 BeautifulSoup 获取它时,<table> 变成空的

python - 定位没有 id 或 class 属性的表

python - 上个月日期时间 Pandas

python - 从 PyIDispatch 调用方法

python - 使用 concurrent.futures 一次消耗许多出队消息

linux - FInd patternf 用于多个文件模式