Issue
Hello i have the following html that i am getting from a beautifulsoup request
<select class="select" name="Type">
<option value="1">Test A</option>
<option value="2">Test B</option>
<option value="3">Test C</option>
<option value="4">Test D</option>
</select>
I have the following code to get me this and i can loop through it and only print out the values, but i can't print out the corresponding text for it. If i want to get the text, i would need to loop it adding the stripped_strings, but then i can only get the text, not the corresponding value. Is there a way to get both
soup = BeautifulSoup(response.content, 'html.parser')
list_a = soup.find('select', {'name':'Type'})
# This will get me only the text
for i in list_a.stripped_strings:
print(i)
# This will get me only the values
list = list_a.find_all('option')
for x in list:
val = x.get('value')
print(str(val))
Solution
This will get you what you need:
from bs4 import BeautifulSoup as bs
html = '''
<select class="select" name="Type">
<option value="1">Test A</option>
<option value="2">Test B</option>
<option value="3">Test C</option>
<option value="4">Test D</option>
</select>
'''
soup = bs(html)
options = soup.select_one('select[name="Type"]').select('option')
for op in options:
print(op.get_text(), '|', op.get('value'))
Result in terminal:
Test A | 1
Test B | 2
Test C | 3
Test D | 4
BeautifulSoup docs can be found here: https://beautiful-soup-4.readthedocs.io/en/latest/index.html
Answered By - Barry the Platipus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.