0

Im trying to print the text after the value= field so far the output looks like this.

<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>

My code looks like this.

soup = BeautifulSoup(html, 'lxml')

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

print(contact)

How can I print the text following the "value=" so just John Smith

Thanks!

Arron
  • 27
  • 7

1 Answers1

0

It is faster to use an id when using CSS selectors and should be your first choice when available (And truly unique on page). You can then use .get to access the value of the value attribute of the matched element.

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'))
QHarr
  • 83,427
  • 12
  • 54
  • 101