简体   繁体   中英

How can I get the selected value in a drop-down list using beautifulsoup?

I am a new learner of web crawling, and I have encountered a problem of getting the selected value in a drop-down list. I am currently using beautifulsoup to try to solve my problem.

The source was like:

<TDalign="right">投资方式:   


    </TD>
<TD>

<SELECT name="financingType">

<OPTION selected="selected"value="48">增资扩股</OPTION>
<OPTION value="211">股权受让</OPTION>
<OPTION value="212">增资+受让</OPTION>
<OPTION value="771">CB</OPTION>
<OPTION value="772">担保</OPTION>

</SELECT>

for this case, I want to get the text marked with value tag "48" (the selected option),that is, "增资扩股".

Honestly, I don't have any idea of this. I have tried:

financingType = soup.find('select',{'name': 'financingType'}).get('value') 

but it gives a NONE value.

I would like to ask through what methods can I get the selected value of this dropdown list?

The problem is that I have many attributes of these "dropdown" type, for example, in this case, I have . And I would like to ask how could I spot this specific attribute of "financingType"?

Thank you very much.

Use css selector which is much faster to fetch the value.

from bs4 import BeautifulSoup

data='''<TDalign="right">投资方式:   


    </TD>
<TD>

<SELECT name="financingType">

<OPTION selected="selected" value="48">增资扩股</OPTION>
<OPTION value="211">股权受让</OPTION>
<OPTION value="212">增资+受让</OPTION>
<OPTION value="771">CB</OPTION>
<OPTION value="772">担保</OPTION>

</SELECT>'''

soup=BeautifulSoup(data,'lxml')
print(soup.select_one('option[selected="selected"]')['value'])
print(soup.select_one('option[selected="selected"]').text)

Printed on console:

    48
增资扩股

EDITED

print(soup.select_one('select[name="financingType"]').select_one('option[selected="selected"]').text)

You can locate the selected option tag with soup.find , specifying the value of the selected attribute:

from bs4 import BeautifulSoup as soup
result = soup(html, 'html.parser').find('select', {'name':'financingType'}).find('option', {'selected':'selected'})['value']

Output:

'48'

You can do this rather easily with mechanicalsoup The solution would look something like that

import mechanicalsoup
bro = mechanicalsoup.StatefulBrowser()
bro.open(url)
bro.select_form(f'form[action={action_name}]')
bro["financingType"] = "48"

You will need to find the form action in the html and set it to a variable action_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM