Home > Net >  how to check if al <li> tag consists an <img> tag and then run code if it exists
how to check if al <li> tag consists an <img> tag and then run code if it exists

Time:07-05

I undertook a web scraping project. There was a ul tag which consisted several li tags. My task was to print the content of the li tag only if the particular li tag consisted an img tag. i'm unable to do so. Dont know whats wrong with my code. Here's my code

from bs4 import BeautifulSoup
import requests

#get the url of the site
url = 'https://devices.ubuntu-touch.io/'

#make a GET request
req = requests.get(url)

#print stau code
print(req.status_code)

# parse the html document
soup = BeautifulSoup(req.content, 'html.parser')

device_list = soup.find_all('li', class_ = 'device-name')
for device in device_list:
    device = device.find('div', class_ = 'text-center col-4')
    if device == 'none':
       model_name = device.find('div', class_ = 'col-8').text
        print(model_name)
    else:
        print('failure')

CodePudding user response:

You need to check if you will find a <img> in your device object:

...
for device in device_list:
    if device.find('img'):
...

As an alternative you could select more specific to pick only these with <img> via css selectors:

for device in soup.select('li.device-name:has(img)'):
    print(device.find('div', class_ = 'col-8').text.strip())
Example
from bs4 import BeautifulSoup
import requests

url = 'https://devices.ubuntu-touch.io/'
req = requests.get(url)

soup = BeautifulSoup(req.content)

device_list = soup.find_all('li', class_ = 'device-name')
for device in device_list:
    if device.find('img'):
        model_name = device.find('div', class_ = 'col-8').text.strip()
        print(model_name)
    else:
        print('failure')

Output

Google Pixel 3a/3a XL
failure
failure
failure
failure
failure
failure
Oneplus One
failure
Volla Phone
failure
failure
failure
failure
failure
Sony Xperia X (F5121 & F5122)
failure
...
  • Related