Home > Software design >  Scraping from a page where the value is a selection fro a drop down menu
Scraping from a page where the value is a selection fro a drop down menu

Time:10-30

I have following html code:

<div class="6u">
                <select name="PropertyId" tabindex="13">
        <option value='0'>Select Property</option>
        <option value='190' >Alderton Crescent</option>
        <option value='9' >Cambridge Way</option>
        <option value='11' selected>Corney Avenue</option>
        <option value='8' >Stanley Gardens</option>                                      
    </select>
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

I am trying to extract only the selected option- in this case Corney Avenue. the code which i'm using:

soup.find('select', {'name': 'PropertyId'}).find_all({'option': 'selected'})

returns almost the same but into kind of a list:

[<option value="0">Select Property</option>
<option value='0'>Select Property</option>
<option value='190' >Alderton Crescent</option>
<option value='9' >Cambridge Way</option>
<option value='11' selected>Corney Avenue</option>
<option value='8' >Stanley Gardens</option>]

CodePudding user response:

This should work-

from bs4 import BeautifulSoup

html="""<div >
                <select name="PropertyId" tabindex="13">
        <option value='0'>Select Property</option>
        <option value='190' >Alderton Crescent</option>
        <option value='9' >Cambridge Way</option>
        <option value='11' selected>Corney Avenue</option>
        <option value='8' >Stanley Gardens</option>                                      
    </select>
</div>"""

soup=BeautifulSoup(html,'lxml')

#find the select tag
select=soup.find('select',attrs={'name':'PropertyId'})

#find option value in select tag where 'selected' attribute is present
value=select.find('option',attrs={'selected':True})

print(value.text)

CodePudding user response:

Alternativ approach could be to use the css selector in combination with .text / get_text() - Following line will grab <option> that is selected and part of <select> named "PropertyId":

soup.select_one('select[name="PropertyId"] option:checked').text

Example

from bs4 import BeautifulSoup

html='''<div >
                <select name="PropertyId" tabindex="13">
        <option value='0'>Select Property</option>
        <option value='190' >Alderton Crescent</option>
        <option value='9' >Cambridge Way</option>
        <option value='11' selected>Corney Avenue</option>
        <option value='8' >Stanley Gardens</option>                                      
    </select>
</div>'''

soup=BeautifulSoup(html,'lxml')

soup.select_one('select[name="PropertyId"] option:checked').text

Output

Corney Avenue
  • Related