Как я могу включить строки, в которых присутствуют не все элементы zip-цикла?

#python #for-loop #beautifulsoup #python-requests #zip

#python #для цикла #beautifulsoup #python-запросы #zip

Вопрос:

Как я могу изменить этот скрипт для печати строк, даже если все элементы отсутствуют? Например, последняя марка и деталь не напечатаны.

 from bs4 import BeautifulSoup
import requests


url = "https://www.rockauto.com/en/catalog/ford,2015,f-150,3.5l v6 turbocharged,3308773,brake amp; 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:

Стратегия с zip() здесь не подходит. Сначала выберите родительский элемент и из этого родительского элемента выберите различную информацию. Если информация отсутствует, замените ее - строкой. Например:

 import requests
from bs4 import BeautifulSoup


url = "https://www.rockauto.com/en/catalog/ford,2015,f-150,3.5l v6 turbocharged,3308773,brake amp; 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 amp; 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 amp; Tow; Carbon-Fiber Ceramic; Includes Hardware
MOTORCRAFT           BRSD1602             Rear; w/ Mechanical Parking Brake; Severe Duty Pads                              -
POWER STOP           Z362087              Front                                                                            Z36 Severe Duty Truck amp; 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
  

РЕДАКТИРОВАТЬ: Чтобы сохранить в csv, вы можете сделать:

 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 amp; 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 (скриншот из LibreOffice):

введите описание изображения здесь

Комментарии:

1. Спасибо за это объяснение. Как я могу сохранить содержимое в файл CSV?

2. Хорошо, я понимаю. Я изменил сценарий, чтобы адаптировать это, но я не могу использовать глобальную переменную в ответе, который сохраняется в файле csv. Например, если после URL я хочу установить vehicle = url[36:] и напечатать это в первом столбце, как я могу это сделать?

Ответ №2:

Вы могли бы поместить эти строки кода

 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