मैं इस स्क्रिप्ट को लाइनों को प्रिंट करने के लिए कैसे संशोधित कर सकता हूं, भले ही सभी आइटम मौजूद न हों? उदाहरण के लिए, अंतिम ब्रांड और भाग मुद्रित नहीं होते हैं।

from bs4 import BeautifulSoup
import requests


url = "https://www.rockauto.com/en/catalog/ford,2015,f-150,3.5l+v6+turbocharged,3308773,brake+&+wheel+hub,brake+pad,1684"

response = requests.get(url)

data = response.text

soup = BeautifulSoup(data, 'html.parser')

linecodes = soup.find_all('span', class_='listing-final-manufacturer')
partnos = soup.find_all('span', class_='listing-final-partnumber as-link-if-js buyers-guide-color')
descriptions = soup.find_all('span', class_='span-link-underline-remover')
infos = soup.find_all('div', class_='listing-text-row')

for linecode,partno,info,description in zip(linecodes,partnos,infos,descriptions):
    result = linecode.text + " | " + partno.text + " | " + info.text + " | " + description.text
    print(result)
1
mjbaybay7 2 सितंबर 2020, 07:34

2 जवाब

सबसे बढ़िया उत्तर

zip() वाली रणनीति यहां उपयुक्त नहीं है। सबसे पहले, मूल तत्व का चयन करें और इस मूल तत्व से विभिन्न जानकारी का चयन करें। यदि जानकारी गुम है, तो उसे - स्ट्रिंग से बदलें। उदाहरण के लिए:

import requests
from bs4 import BeautifulSoup


url = "https://www.rockauto.com/en/catalog/ford,2015,f-150,3.5l+v6+turbocharged,3308773,brake+&+wheel+hub,brake+pad,1684"

response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, 'html.parser')

linecodes = []
partnos = []
descriptions = []
infos = []
for tbody in soup.select('tbody[id^="listingcontainer"]'):
    tmp = tbody.find('span', class_='listing-final-manufacturer')
    linecodes.append(tmp.text if tmp else '-')

    tmp = tbody.find('span', class_='listing-final-partnumber as-link-if-js buyers-guide-color')
    partnos.append(tmp.text if tmp else '-')

    tmp = tbody.find('span', class_='span-link-underline-remover')
    descriptions.append(tmp.text if tmp else '-')

    tmp = tbody.find('div', class_='listing-text-row')
    infos.append(tmp.text if tmp else '-')


for row in zip(linecodes,partnos,infos,descriptions):
    print('{:<20} {:<20} {:<80} {}'.format(*row))

प्रिंट:

CENTRIC              30016020             Rear; w/ Manual parking brake                                                    Semi-Metallic; w/Shims and Hardware
CENTRIC              30116020             Rear; w/ Manual parking brake                                                    Ceramic; w/Shims and Hardware
DYNAMIC FRICTION     1551160200           Rear; Manual Parking Brake                                                       5000 Advanced; Ceramic
BENDIX               CFC1790              Rear; w/ Power Parking Brake                                                     Copper Free Ceramic; Hardware Not Included
BENDIX               CFC1602              Rear; w/ Manual parking brake                                                    Copper Free Ceramic; Hardware Included
CENTRIC              10416020             Rear; w/ Manual parking brake                                                    Posi-Quiet; Semi-Metallic; w/Shims and Hardware
DYNAMIC FRICTION     1551179000           Rear; Electric Parking Brake                                                     5000 Advanced; Ceramic
POWER STOP           161602               Rear; With mechanical parking brake                                              Z16 Evolution Clean Ride Ceramic
POWER STOP           171790               Rear; with Electric Parking Brake                                                Z17 Evolution Clean Ride Ceramic with Hardware
CENTRIC              30117900             Rear; w/ Electronic parking brake                                                Ceramic; w/Shims and Hardware
ACDELCO              17D1790CH            Rear; w/ Power Parking Brake                                                     Professional; Ceramic; w/Hardware
BOSCH                BC1602               Rear; Mechanical PB                                                              QuietCast; Ceramic; Includes Hardware
POWER STOP           161790               Rear; with Electric Parking Brake                                                Z16 Evolution Clean Ride Ceramic
WAGNER               QC1602               Rear; Manual Parking Brake; OE Ceramic                                           ThermoQuiet; Includes Installation Hardware; Ceramic
POWER STOP           171602               Rear; With mechanical parking brake                                              Z17 Evolution Clean Ride Ceramic with Hardware
POWER STOP           162087               Front                                                                            Z16 Evolution Clean Ride Ceramic
WAGNER               QC1414               Front; OE Ceramic                                                                ThermoQuiet; Includes Installation Hardware; Ceramic
CENTRIC              10516020             Rear; w/ Manual parking brake                                                    Posi-Quiet; Ceramic; w/Shims and Hardware
BOSCH                BC1790               Rear; w/ Electronic parking brake                                                QuietCast; Ceramic; Includes Hardware
POWER STOP           172087               Front                                                                            Z17 Evolution Clean Ride Ceramic with Hardware
ACDELCO              17D1602CHF1          Rear; Manual Parking Brake; Revised F1 Part Design                               Professional; Ceramic; w/Hardware
CENTRIC              10517900             Rear; w/ Electronic parking brake                                                Posi-Quiet; Ceramic; w/Shims and Hardware
BENDIX               CFC1770              Front                                                                            Copper Free Ceramic; Hardware Included
CENTRIC              30114140             Front                                                                            Ceramic; w/Shims and Hardware
MOTORCRAFT           BR1790               Rear; w/ Electric Parking Brake; w/ Standard Duty Pads (Only 1 Remaining)        Integrally Molded
AKEBONO              ACT1602              Rear; w/Manual Parking Brake; OE Pad Material is Ceramic                         ProACT Ceramic Pad w/ Stainless Hardware
AKEBONO              ACT1790              Rear; w/Electric Parking Brake; OE Pad Material is Ceramic (Only 1 Remaining)    ProACT Ceramic Pad w/ Stainless Hardware
DYNAMIC FRICTION     1551177000           FitsFront; Manual Parking BrakeOrFront; Electric Parking Brake                   5000 Advanced; Ceramic
CENTRIC              10514140             Front                                                                            Posi-Quiet; Ceramic; w/Shims and Hardware
MOTORCRAFT           BR1602A              Rear; w/ Mechanical Parking Brake                                                Service Design
BOSCH                BC1770               Front                                                                            QuietCast; Ceramic; Includes Hardware
BREMBO               P24168N              Rear                                                                             NAO Ceramic
MOTORCRAFT           BRF1469              Rear; w/ Electric Parking Brake                                                  OE Design
ACDELCO              17D1414CH            Front                                                                            Professional; Ceramic; w/Hardware
AKEBONO              ACT1414              Front; OE Pad Material Is Ceramic                                                ProACT Ceramic Pad w/ Stainless Hardware
MOTORCRAFT           BRF1550              Rear; w/ Mechanical Parking Brake                                                OE Design
MOTORCRAFT           BRF1856              Front; w/ Traction Pack II                                                       OE Design
DYNAMIC FRICTION     1551160201           Rear; Manual Parking Brake                                                       5000 Advanced; Ceramic; Includes Hardware Kit
DYNAMIC FRICTION     1551179001           Rear; Electric Parking Brake                                                     5000 Advanced; Ceramic; Includes Hardware Kit
DYNAMIC FRICTION     1551177001           FitsFront; Manual Parking BrakeOrFront; Electric Parking Brake                   5000 Advanced; Ceramic; Includes Hardware Kit
CENTRIC              30014140             Front (Only 1 Remaining)                                                         Semi-Metallic; w/Shims and Hardware
CENTRIC              10414140             Front                                                                            Posi-Quiet; Semi-Metallic; w/Shims and Hardware
CENTRIC              30117700             Front                                                                            Ceramic; w/Shims and Hardware
CENTRIC              10517700             Front                                                                            Posi-Quiet; Ceramic; w/Shims
MOTORCRAFT           BR1790A              Rear; w/ Electric Parking Brake                                                  Service Design
CENTRIC              10316020             Rear; w/ Manual parking brake                                                    C-TEK; Ceramic
DURAGO               BP1602C              Rear; Manual Parking Brake                                                       Ceramic
BENDIX               SBC1602              Rear; w/ Manual parking brake                                                    Stop Ceramic; Hardware Included
WAGNER               ZD1602               Rear; Manual Parking Brake; OE Ceramic                                           QuickStop; Includes Installation Hardware; Ceramic
BENDIX               SBC1790              Rear; w/ Power Parking Brake                                                     Stop Ceramic; Hardware Not Included
WAGNER               ZD1790               Rear; with Electronic Parking Brake; OE Ceramic                                  QuickStop; Includes Installation Hardware; Ceramic
BOSCH                BE1602H              Rear; Mechanical PB                                                              Bosch Blue w/ Hardware
CENTRIC              10214140             Front                                                                            C-TEK; Metallic
WAGNER               ZD1414               Front; OE Ceramic                                                                QuickStop; Includes Installation Hardware; Ceramic
RAYBESTOS            MGD1602CH            Rear; Manual Parking Brake                                                       R-Line; Ceramic
BOSCH                BE1790H              Rear; w/ Electronic parking brake                                                Bosch Blue w/ Hardware
CENTRIC              10314140             Front                                                                            C-TEK; Ceramic
DURAGO               BP1414C              Front                                                                            Ceramic
RAYBESTOS            MGD1414CH            Front                                                                            R-Line; Ceramic
BENDIX               SBC1770              Front                                                                            Stop Ceramic; Hardware Included
ACDELCO              14D1602CH            Rear; Manual Parking Brake                                                       Advantage; Ceramic; w/Hardware
BOSCH                BE1770H              Front                                                                            Bosch Blue w/ Hardware
ACDELCO              14D1414CH            Front                                                                            Advantage; Ceramic; w/Hardware
MOTORCRAFT           NBR1602              Rear; w/ Mechanical Parking Brake                                                Service Design
DYNAMIC FRICTION     1311160200           Rear; Manual Parking Brake                                                       3000 Semi-Metallic
DYNAMIC FRICTION     1310160200           Rear; Manual Parking Brake                                                       3000 Ceramic
DYNAMIC FRICTION     1311179000           Rear; Electric Parking Brake                                                     3000 Semi-Metallic
DYNAMIC FRICTION     1310179000           Rear; Electric Parking Brake                                                     3000 Ceramic
DYNAMIC FRICTION     1311177000           FitsFront; Manual Parking BrakeOrFront; Electric Parking Brake                   3000 Semi-Metallic
DYNAMIC FRICTION     1310179001           Rear; Electric Parking Brake                                                     3000 Ceramic; Includes Hardware Kit
DYNAMIC FRICTION     1310177000           FitsFront; Manual Parking BrakeOrFront; Electric Parking Brake                   3000 Ceramic
RAYBESTOS            MGD1790CH            Rear; w/ Power Parking Brake                                                     R-Line; Ceramic
BENDIX               MKD1790FM            Rear; w/ Power Parking Brake                                                     Fleet Metlok; Semi-Metallic; Hardware Not Included
RAYBESTOS            SP1790TRH            Rear; w/ Power Parking Brake                                                     Specialty - Truck; Ceramic
ACDELCO              17D1790SDH           Rear; w/ Power Parking Brake                                                     Severe Duty; Ceramic
RAYBESTOS            SP1602TRH            Rear; Manual Parking Brake                                                       Specialty - Truck; Ceramic
BENDIX               MKD1770FM            Front                                                                            Fleet Metlok; Semi-Metallic; Hardware Included
DYNAMIC FRICTION     1214177000           FitsFront; Manual Parking BrakeOrFront; Electric Parking Brake                   Heavy Duty Pads; Semi Metallic
WAGNER               SX1414               Front                                                                            SevereDuty; Includes Installation Hardware; Semi-Metallic
CENTRIC              10616020             Rear; w/ Manual parking brake                                                    Posi-Quiet Extended Wear; w/Shims and Hardware
POWER STOP           Z361602              Rear; With mechanical parking brake                                              Z36 Severe Duty Truck & Tow; Carbon-Fiber Ceramic; Includes Hardware
RAYBESTOS            SP1414TRH            Front                                                                            Specialty - Truck; Ceramic
WAGNER               SX1602               Rear                                                                             SevereDuty; Includes Installation Hardware
MONROE               HDX1602              Rear; w/ Manual Operated Parking Brake (Only 1 Remaining)                        Severe Solution; Includes Hardware Kit
POWER STOP           Z361790              Rear; with Electric Parking Brake                                                Z36 Severe Duty Truck & Tow; Carbon-Fiber Ceramic; Includes Hardware
MOTORCRAFT           BRSD1602             Rear; w/ Mechanical Parking Brake; Severe Duty Pads                              -
POWER STOP           Z362087              Front                                                                            Z36 Severe Duty Truck & Tow; Carbon-Fiber Ceramic; Includes Hardware
CENTRIC              10614140             Front                                                                            Posi-Quiet Extended Wear; w/Shims and Hardware
ACDELCO              17D1414MHSV          Front                                                                            Specialty; Fleet, Semi-Metallic; w/Hardware
ACDELCO              17D1414SDH           Front                                                                            Severe Duty; Ceramic
DYNAMIC FRICTION     1214160200           Rear; Manual Parking Brake                                                       Heavy Duty Pads; Semi Metallic
DYNAMIC FRICTION     1214179000           Rear; Electric Parking Brake                                                     Heavy Duty Pads; Semi Metallic
ACDELCO              17D1602SDH           Rear; Manual Parking Brake                                                       Severe Duty
RAYBESTOS            EHT1790H             Rear; w/ Power Parking Brake                                                     Element3; Hybrid Technology
RAYBESTOS            EHT1602H             Rear; Manual Parking Brake                                                       Element3; Hybrid Technology
CENTRIC              50016020             Rear; w/ Manual parking brake                                                    PQ PRO; w/Shims and Hardware
CENTRIC              50017900             Rear; w/ Electronic parking brake                                                PQ PRO; w/Shims and Hardware
RAYBESTOS            EHT1414H             Front                                                                            Element3; Hybrid Technology
POWER STOP           Z231790              Rear; with Electric Parking Brake                                                Z23 Evolution Sport; Carbon Fiber-Ceramic; Includes Hardware
CENTRIC              50014140             Front                                                                            PQ PRO; w/Shims and Hardware
POWER STOP           Z232087              Front                                                                            Z23 Evolution Sport; Carbon Fiber-Ceramic; Includes Hardware
POWER STOP           Z231602              Rear; With mechanical parking brake                                              Z23 Evolution Sport; Carbon Fiber-Ceramic; Includes Hardware
ADVICS               AD1414               Front; OE Pad Material Is Ceramic                                                Ultra-Premium OE Replacement; Includes Hardware
WAGNER               OEX1602              Rear; Manual Parking Brake; OE Ceramic                                           OEX; Includes Installation Hardware
WAGNER               OEX1790              Rear; with Electronic Parking Brake; OE Ceramic                                  OEX; Includes Installation Hardware
WAGNER               OEX1414              Front; OE Ceramic                                                                OEX; Includes Installation Hardware
AKEBONO              ASP1602              Rear; w/Manual Parking Brake; OE Pad Material is Ceramic                         Performance Ceramic Pad w/ Stainless Hardware
RAYBESTOS            SP1414PSH            Front                                                                            Specialty - Police; Ceramic
CENTRIC              30614140             Front (Only 1 Remaining)                                                         Fleet Performance Pads; w/Hardware
AKEBONO              ASP1414A             Front; OE Pad Material Is Ceramic                                                Performance Ceramic Pad w/ Stainless Hardware
DYNAMIC FRICTION     1000179000           Rear; Electric Parking Brake                                                     Track/Street Pads; Low Metallic
DYNAMIC FRICTION     1400160200           Rear; Manual Parking Brake                                                       Ulitmate Duty Performance
DYNAMIC FRICTION     1400179000           Rear; Electric Parking Brake                                                     Ulitmate Duty Performance

संपादित करें: सीएसवी में सहेजने के लिए, आप यह कर सकते हैं:

import csv
import requests
from bs4 import BeautifulSoup


url = "https://www.rockauto.com/en/catalog/ford,2015,f-150,3.5l+v6+turbocharged,3308773,brake+&+wheel+hub,brake+pad,1684"

response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, 'html.parser')

linecodes = []
partnos = []
descriptions = []
infos = []
for tbody in soup.select('tbody[id^="listingcontainer"]'):
    tmp = tbody.find('span', class_='listing-final-manufacturer')
    linecodes.append(tmp.text if tmp else '-')

    tmp = tbody.find('span', class_='listing-final-partnumber as-link-if-js buyers-guide-color')
    partnos.append(tmp.text if tmp else '-')

    tmp = tbody.find('span', class_='span-link-underline-remover')
    descriptions.append(tmp.text if tmp else '-')

    tmp = tbody.find('div', class_='listing-text-row')
    infos.append(tmp.text if tmp else '-')

with open('data.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for row in zip(linecodes,partnos,infos,descriptions):
        print('{:<20} {:<20} {:<80} {}'.format(*row))
        writer.writerow(row)

बचाता है data.csv (लिब्रे ऑफिस से स्क्रीनशॉट):

enter image description here

1
Andrej Kesely 2 सितंबर 2020, 20:44

आप कोड की ये पंक्तियाँ डाल सकते हैं

linecodes = soup.find_all('span', class_='listing-final-manufacturer')
partnos = soup.find_all('span', class_='listing-final-partnumber as-link-if-js buyers-guide-color')
descriptions = soup.find_all('span', class_='span-link-underline-remover')
infos = soup.find_all('div', class_='listing-text-row')

इस तरह try/except में:

try:
    linecodes = soup.find_all('span', class_='listing-final-manufacturer').text
except:
    linecodes = ''

और फिर .text को कॉल किए बिना इन चरों का उपयोग करें

1
Maxiboi 2 सितंबर 2020, 08:10