如何使用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]}nt{stock[idx]}')

相关内容

最新更新