简体   繁体   中英

How to print "not found' in soup.find_all?

In some of the lines I have the below html code:

<span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">SAR</span>&nbsp;625.00</span> <small class="woocommerce-price-suffix">(Excluding Tax)</small></span>

and in some lines the code is not there. If this code is not available on those lines i want it to print "Not found"

I am using the below code but am not able to get the right answer:

p=soup.findAll("span", {"class":"price"})
for price in p:
  if price in p:
    prices.append(price.text)
  else:
    prices.append("Not found")

can someone please help me out how to solve this issue.

Search for parent element of each product, in this case <div class="product-inner"> and then for price. If price is not found, then set it to "Not found" .

For example:

import requests
from bs4 import BeautifulSoup


url = 'https://www.softland.com.sa/category/brand'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

for product in soup.find_all('div', class_='product-inner'):
    title = product.h2.text
    price = product.find('span', class_='amount')
    if price:
        price = price.text
    else:
        price = 'Not found'
    print('{:<15} {}'.format(price, title))

Prints:

SAR 350.00      SAMSUNG SSD 860 EVO SATA III 2.5 inch 500 GB-MZ-76E500BW-887276231631
SAR 625.00      Samsung 860 EVO 1TB 2.5 Inch SATA III Internal SSD MZ-76E1T0BW-887276231648
Not found       MSI Z390-A PRO 9th Gen. Motherboard
SAR 75.00       ASUS Internal 24X DVD BURNER Black (DRW-24D5MT)
SAR 2,750.00    Gaming Series #1
SAR 485.00      Samsung 970 Evo Plus M.2-500GB-MZ-V7S500BW-887276302546
SAR 395.00      CoolerMaster MasterBox MB520 Red-Trim
SAR 150.00      Xtrike Wired 4 in 1 Combo with ENG/ARABIC layout -CM-400
SAR 350.00      NZXT  Aer RGB 120 Triple Pack  RGB LED PWM Fan for HUE (RF-AR120-T1)
SAR 895.00      Samsung M.2-970 Evo Plus 1TB-MZ-V7S1T0BW-887276302522
SAR 395.00      TEAM DELTA R BLACK UD-D4 8GBx2 3000
SAR 350.00      Samsung M.2-970 Evo Plus 250GB-MZ-V7S250BW-887276302515
SAR 190.00      TEAM Vulcan Z RED UD-D4 8GB 2666
SAR 125.00      NZXT Aer RGB120 Single Pack 120mm Digitally Controlled RGB LED Fans for HUE (RF-AR120-B1)
SAR 190.00      Gamdias Hermes E2 7 Neon Color Mechanical Gaming Keyboard with Blue Switches (87 Keys)
SAR 560.00      Windows 10 Pro 64-bit Arabic – OEM
SAR 935.00      Corsair Crystal Series 570X RGB ATX Mid-Tower Case RED-CC-9011111-WW
SAR 325.00      BitFenix Prodigy M Window Side Panel Computer Case, Black, BFC-PRM-300-RRWKK-RP, Micro ATX / Mini-ITX
SAR 180.00      DEEPCOOL GAMMAXX 200T CPU AirCooler
SAR 480.00      Corsair Vengeance RGB Pro 16GB (2x8GB) DDR4-3200 CMW16GX4M2E3200C16

EDIT: Changed class product to product-inner .


To get the information as pandas DataFrame, you can use this script:

import requests
import pandas as pd
from bs4 import BeautifulSoup

url = 'https://www.softland.com.sa/category/brand'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

all_data = []
for product in soup.find_all('div', class_='product-inner'):
    title = product.h2.text
    price = product.find('span', class_='amount')
    if price:
        price = price.text
    else:
        price = 'Not found'
    all_data.append({'Title': title, 'Price': price})

df = pd.DataFrame(all_data)
print(df)

Prints:

                                                Title         Price
0   SAMSUNG SSD 860 EVO SATA III 2.5 inch 500 GB-M...    SAR 350.00
1   Samsung 860 EVO 1TB 2.5 Inch SATA III Internal...    SAR 625.00
2                 MSI Z390-A PRO 9th Gen. Motherboard     Not found
3     ASUS Internal 24X DVD BURNER Black (DRW-24D5MT)     SAR 75.00
4                                    Gaming Series #1  SAR 2,750.00
5   Samsung 970 Evo Plus M.2-500GB-MZ-V7S500BW-887...    SAR 485.00
6               CoolerMaster MasterBox MB520 Red-Trim    SAR 395.00
7   Xtrike Wired 4 in 1 Combo with ENG/ARABIC layo...    SAR 150.00
8   NZXT  Aer RGB 120 Triple Pack  RGB LED PWM Fan...    SAR 350.00
9   Samsung M.2-970 Evo Plus 1TB-MZ-V7S1T0BW-88727...    SAR 895.00
10                TEAM DELTA R BLACK UD-D4 8GBx2 3000    SAR 395.00
11  Samsung M.2-970 Evo Plus 250GB-MZ-V7S250BW-887...    SAR 350.00
12                   TEAM Vulcan Z RED UD-D4 8GB 2666    SAR 190.00
13  NZXT Aer RGB120 Single Pack 120mm Digitally Co...    SAR 125.00
14  Gamdias Hermes E2 7 Neon Color Mechanical Gami...    SAR 190.00
15                 Windows 10 Pro 64-bit Arabic – OEM    SAR 560.00
16  Corsair Crystal Series 570X RGB ATX Mid-Tower ...    SAR 935.00
17  BitFenix Prodigy M Window Side Panel Computer ...    SAR 325.00
18                DEEPCOOL GAMMAXX 200T CPU AirCooler    SAR 180.00
19  Corsair Vengeance RGB Pro 16GB (2x8GB) DDR4-32...    SAR 480.00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM