Как получить данные при парсе в python?

как сделать запрос, если в одном классе допустим несколько <a href как выбрать тот который мне нужен? Пытаюсь из div класса "content_product_catalog_list_item_picture" забрать ссылку <a href на станицу товара, но необходимо выбрать определенную, так как после div, <a href всречается 3 раза

import requests
from bs4 import BeautifulSoup
import csv

HOST = 'https://happywear.ru/'
URL = 'https://happywear.ru/boys/boy-povsednevnaya-odegda/boy-shtani'
HEADERS = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
}

def get_html(url, params=''):
    r = requests.get(url, headers=HEADERS, params=params)
    return r

def get_content(html):
    soup = BeautifulSoup(html, 'html.parser')
    items = soup.find_all('li', class_='js-catalog-item content_product_catalog_list_item')
    cards = []

    for item in items:
        cards.append(
            {

                'title': item.find('a', class_='content_product_catalog_list_item_info_introtext').get_text(strip=type),
                'title_2': item.find('div', class_='content_product_catalog_list_item_picture').find('a').get_text('href'),
                'title_rub': item.find('div', class_='content_product_catalog_list_item_price__block').get_text(strip=type),
                'card_img': item.find('div', class_='content_product_catalog_list_item_picture').find('img').get('src')
            }
        )
    return cards 

html = get_html(URL)
print(get_content(html.text))

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


Ответы (1 шт):

Автор решения: Іван Шнір

Самым лёгким и быстрым решением в данной ситуации будет спарсить все 3 ссылки и перебрать вам нужную.

Показываю на примере:

Main_hrefs = []
three_hrefs = [fail, fail, true]
# Нам надо забрать True так что для теста выводим весь массив
print(three_hrefs)
# Увидели что наша ссылка в конце и засовываем эту ячейку в нужный нам массив с ссылками
Main_hrefs.append(three_hrefs[2])
→ Ссылка