在某些行中,我有以下html代码:
<span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">SAR</span> 625.00</span> <small class="woocommerce-price-suffix">(Excluding Tax)</small></span>
而在某些行中,代码并不存在。 如果这段代码在这些行上不可用,我希望它打印“not found”
我正在使用下面的代码,但无法得到正确的答案:
p=soup.findAll("span", {"class":"price"})
for price in p:
if price in p:
prices.append(price.text)
else:
prices.append("Not found")
谁能帮我解决这个问题。
搜索每个产品的父元素,在本例中为 例如: 打印:“not found”
。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'):
title = product.h2.text
price = product.find('span', class_='amount')
if price:
price = price.text
else:
price = 'Not found'
print('{:<15} {}'.format(price, title))
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
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
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
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
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
在某些行中,我有以下html代码:
<span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">SAR</span> 625.00</span> <small class="woocommerce-price-suffix">(Excluding Tax)</small></span>
而在某些行中,代码并不存在。 如果这段代码在这些行上不可用,我希望它打印“not found”
我正在使用下面的代码,但无法得到正确的答案:
p=soup.findAll("span", {"class":"price"})
for price in p:
if price in p:
prices.append(price.text)
else:
prices.append("Not found")
谁能帮我解决这个问题。