di中的Beautifulsoup打印值

2024-10-16 17:28:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试打印value=字段后面的文本,到目前为止,输出如下所示。在

<div class="controls"><input class="span12 text-bound" id="client_appbundle_prospecttype_name" maxlength="100" name="client_appbundle_prospecttype[name]" required="required" type="text" value="John Smith"/></div>

我的代码是这样的。在

^{pr2}$

如何打印“value=”后面的文本,以便只打印John Smith

谢谢!在


Tags: textname文本divclientinputvaluerequired
1条回答
网友
1楼 · 发布于 2024-10-16 17:28:44

在使用CSS选择器时使用id会更快,而且在可用时应该是您的首选(并且在页面上是唯一的)。然后可以使用.get访问匹配元素的value属性的值。在

from bs4 import BeautifulSoup
html = '<div class="controls"><input class="span12 text-bound" id="client_appbundle_prospecttype_name" maxlength="100" name="client_appbundle_prospecttype[name]" required="required" type="text" value="John Smith"/></div>'
soup = BeautifulSoup(html, "lxml")
print(soup.select_one('#client_appbundle_prospecttype_name').get('value'))

相关问题 更多 >