如何使用Beautifulsoup在input元素中添加属性

时间:2016-10-17 10:40:40

标签: python forms beautifulsoup

我使用BeautifulSoup在我的html页面上查找特定元素。 我想添加一个属性" value"到这个元素并将其保存到原始的html。 我怎么能用Beautifulsoup做到这一点?现在我这样做是为了得到整个html并找到具体的元素:

static_map = opener.open('my_url')
bs = BeautifulSoup(static_map.read())
title = bs.find("input", {"name":"title"})

标题如下:

<input class="has-popover form-control" data-container="body" id="id_title" maxlength="255" name="title" type="text"/>

我想在此input元素中添加属性:value 然后将其保存到最初的html。

然后我必须将此作为发布请求发送。

1 个答案:

答案 0 :(得分:1)

请改为尝试:

bs.find('input')['value'] = ''#Whatever you want the value to be.

因为bs.find返回一个字典,所以要用dicionary设置一个项使用下标表示法。