【问题标题】:Beautifulsoup print value in div [duplicate]div中的Beautifulsoup打印值[重复]
【发布时间】:2019-04-24 01:41:41
【问题描述】:

到目前为止,我正在尝试在 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>

我的代码如下所示。

soup = BeautifulSoup(html, 'lxml')

contact = soup.find('div', {"class": "controls"})

print(contact)

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

谢谢!

【问题讨论】:

    标签: python selenium beautifulsoup screen-scraping


    【解决方案1】:

    在使用 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'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      相关资源
      最近更新 更多