Reputation: 2686
I'm looking to modify my existing code which currently parses td
attributes of a webpage source, and switch that out for the value
of an option
field equals a numerical value. Let me explain
import requests, re, collections
from bs4 import BeautifulSoup
def get_content(url):
if type(url) != str:
print('You need to included a string')
exit()
else:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0',
'From': '[email protected]'
}
req = requests.get(url,headers=headers)
soup = BeautifulSoup(req.content, 'html.parser')
current_month=soup.find_all('td', {'id': 'monatevent'})
fwk_nextmonth=soup.find_all('td', {'id': 'aevent'})
curr_month = []
fwk_next_month = []
I want to now parse the following select
option
attribute to alert when option value=08
for example:
<select name="month" onchange="submit()">
<option value="09" selected="">09</option>
<option value="10">10</option><option value="11">11</option>
</select>
It appears if I use the following selector in my code, that nothing is returned: current_month=soup.find_all('select', {'option': '08'})
Can someone please help? Thank you.
Upvotes: 0
Views: 1038
Reputation: 195553
You can use CSS selectors built-in within BeautifulSoup. Selector option[selected]
will find tag <option>
with attribute selected
:
data = """<select name="month" onchange="submit()">
<option value="09" selected="">09</option>
<option value="10">10</option><option value="11">11</option>
</select>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'lxml')
print(soup.select_one('option[selected]').text)
Prints:
09
Edit:
If you want to find option with value=08, you can do CSS selector `option[value=08]`:
data = """<select name="month" onchange="submit()">
<option value="08">08</option>
<option value="09" selected="">09</option>
<option value="10">10</option><option value="11">11</option>
</select>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'lxml')
print(soup.select_one('option[value=08]'))
Prints:
<option value="08">08</option>
Upvotes: 1
Reputation: 573
I think you try to find the value of selected option, as from html string. May be this help..
from bs4 import BeautifulSoup
html_str = """<select name="month" onchange="submit()">
<option value="09" selected="">09</option>
<option value="10">10</option><option value="11">11</option>
</select>"""
soup = BeautifulSoup(html_str, 'html.parser')
select = soup.find('select')
for option in select.find_all('option'):
if option.has_attr('selected'):
print('Value:', option.get('value'))
Upvotes: 0