Issue
I have a trouble parsing html. I am working with a website that have some items in a list with different class names. What I'm trying to do is find them all in a single findAll like this:
page_soup.findAll("li", {"Class" : "Class1" or "Class2"})
I want to have "OR" between my classes.
Sample html:
<ol class="products-list" id="products">
<li class="item odd">
</li>
<li class="item even">
</li>
<li class="item last even">
</li>
</ol>
Solution
Use Select
() which is faster than findAll
()
page_soup=BeautifulSoup(html,'html.parser')
for item in page_soup.select(".odd,.even"):
print(item)
Code here:
from bs4 import BeautifulSoup
html='''<ol class="products-list" id="products">
<li class="item odd">
</li>
<li class="item even">
</li>
<li class="item last even">
</li>
</ol>
'''
page_soup=BeautifulSoup(html,'html.parser')
for item in page_soup.select(".odd,.even"):
print(item)
Answered By - KunduK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.