Home > Mobile >  How print li strong without class or id in beautifulsoup
How print li strong without class or id in beautifulsoup

Time:06-01

I have this code for scrap '1.6.3'

<div >
<ul>
<li>Very cheap price &amp; Original product !</li>
<li><strong>Product Version :</strong> 1.6.3</li>
<li><strong>Product Last Updated :</strong> 08.12.2021</li>
</ul>
</div>

I havent id or class in li or strong. This is my code.

        version_soup = soup_linke.find(class_='product-short-description') 


        strong_items = version_soup.find_all('strong')
        li_items = version_soup.find_all('li')
       
        for i,z in zip(strong_items, li_items):
            if i.get_text() == 'Product Version :':
                print(z.text)
            else:
                continue

CodePudding user response:

Sorry. But i wrote this.

version_soup = soup_linke.find(class_='product-short-description') 
        li_items = version_soup.find_all('li')
        version_li = []
        for i in li_items:
            listas = i.text
            version_li.append(listas.strip('Product Version: '))
        print(version_li[2])
Output: 1.6.3

CodePudding user response:

To print the <li> text without the <strong> text a generic approach would be to use:

.find(text=True, recursive=False)
Example
from bs4 import BeautifulSoup

html='''
<div >
<ul>
<li>Very cheap price &amp; Original product !</li>
<li><strong>Product Version </strong> 1.6.3</li>
<li><strong>Product Last Updated </strong> 08.12.2021</li>
</ul>
</div>
'''
soup = BeautifulSoup(html)

for e in soup.select('.product-short-description li'):
    print(e.find(text=True, recursive=False).strip())
Output
Very cheap price & Original product !
1.6.3
08.12.2021
  • Related