如何使用 python 提取具有相同 class 的所有数据

How to extract all data having same class using python

我需要找到所有的标题数据和库存编号。我写了一个代码,当我使用 find_all 方法时,它对单个项目运行良好,它显示错误,请查看我的代码并指导我如何处理这个问题。谢谢!

这是我的代码:

import requests
from bs4 import BeautifulSoup
#import pandas as pd
#import numpy as
import csv

def get_page(url):
    response = requests.get(url)
    if not response.ok:
        print('server responded:', response.status_code)
    else:
        soup = BeautifulSoup(response.text, 'html.parser') # 1. html , 2. parser
    return soup
def get_detail_page(soup):
     title = soup.find_all('div',class_="vehicle-location-name mts bold",id=False).text
     print(title)
     stock = soup.find_all('div',class_="text-lightgray",id=False).find('span').text
     print(stock)
def main():
    url = "https://www.superbrightleds.com/vehicle/2002-acura-cl-vehicle-led-lights?make=1&model=554&year=2002"
    get_detail_page(get_page(url))

if __name__ == '__main__':
    main()

尝试:

def get_detail_page(soup):
    titles = soup.findAll('div', attrs={"class": "vehicle-location-name mts bold"})
    stocks = soup.findAll('div', attrs={"class": "text-lightgray"})

    title = [title.get_text() for title in titles if title]
    stock = [stock.get_text() for stock in stocks if stock and 'Stock #' in str(stock)]

    for idx in range(len(stock)):
        print(f'{title[idx]}\n\t{stock[idx]}')