Search code examples
htmlbeautifulsouptagsgettext

I need to extract an element from a tag using BeautifulSoup4


Simply put, I have the below tag in my soup and I need to extract the date from the 'value' element.

<input class="mm-date-field form-control input-sm date-picker" title="Select as of date" type="text" value="03/31/2023"/>

I've tried a number of things after searching online, but my latest attempts were the below:

date = cmbs_soup.find('input', title = 'Select as of date', type = 'text', value = re.compile('.*'))
date.text

date = cmbs_soup.find('input', {"class": "mm-date-field form-control input-sm date-picker", "title": "Select as of date", "type": "text"}).get_text("value")
print(date)

Both resulted in '' - an empty string. What am I missing?!?!

Ty!


Solution

  • You can get any tag attribute by treating it like a dictionary - with <Tag>.get(<attr>) [if you're not sure the element will have that attribute] or <Tag>[<attr>] [if you are sure that the element has that key].

    If you find the element with

    date = cmbs_soup.find('input', title='Select as of date', type='text', value=True) # OR
    # date = cmbs_soup.select_one('input.mm-date-field.form-control.input-sm.date-picker[title="Select as of date"][type="text"][value]')
    

    then you can get the value attribute with date['value'] or date.get('value') or date.attrs['value'] or date.attrs.get('value').