I am trying to get all text from all options element. here is html
y = soup.find('div',{'class':'options push-select push-image push-checkbox push-radio'})
print(y)
<div class="options push-select push-image push-checkbox push-radio"><h3>Available Options</h3> <br/><div class="option option-select" id="option-2713"> <span class="required">*</span> <b>Size:</b><br/> <select name="option[2713]"><option value=""> --- Please Select ---</option><option data-image="https://www.pursemall.ru/image/cache/no_image-30x30h.jpg" value="16844">85CM</option><option data-image="https://www.pursemall.ru/image/cache/no_image-30x30h.jpg" value="16845">90CM</option><option data-image="https://www.pursemall.ru/image/cache/no_image-30x30h.jpg" value="16846">95CM</option><option data-image="https://www.pursemall.ru/image/cache/no_image-30x30h.jpg" value="16847">100CM</option><option data-image="https://www.pursemall.ru/image/cache/no_image-30x30h.jpg" value="16848">105CM</option><option data-image="https://www.pursemall.ru/image/cache/no_image-30x30h.jpg" value="16849">110CM</option><option data-image="https://www.pursemall.ru/image/cache/no_image-30x30h.jpg" value="16850">115CM</option><option data-image="https://www.pursemall.ru/image/cache/no_image-30x30h.jpg" value="16851">120CM</option> </select></div> <br/></div>
I tried this code but didn't work:
for i in y:
i = i.find_all('option')
print(i)
>>>[]
my expected result will be:
85CM,90CM,95CM.....
CodePudding user response:
What happens
y
ist not a list
cause find()
return type will be <class 'bs4.element.Tag'>
.
Whereas find_all()
returns all the matches from document and return type will be <class 'bs4.element.ResultSet'>
- See also docs
Solution
So you should iterate over y.find_all('option')
:
for i in y.find_all('option'):
print(i.text)
Output
--- Please Select ---
85CM
90CM
95CM
100CM
105CM
110CM
115CM
120CM
Additiv info
To avoid the default option you can slice the result:
y.find_all('option')[1:]
or to genrate a list with text values:
[x.text for x in y.find_all('option')[1:]]