Не могу получить атрибут

Рейтинг: 0Ответов: 1Опубликовано: 25.02.2023

Не могу получить атрибут href и записать в json, помогите пожалуйста

import time
import requests
from bs4 import BeautifulSoup
import json
import lxml
from selenium.webdriver.common.by import By
from selenium import webdriver

options = webdriver.ChromeOptions()
options.headless = False
driver = webdriver.Chrome(executable_path='chromedriver.exe', options=options)



try:

    mainurl = f'https://youla.ru/all?q=наушники'
    driver.get(url=mainurl)
    print('Открыл браузер')
    time.sleep(20)
    html = driver.page_source
    with open('youla.html', 'w', encoding='utf-8') as file:
        file.write(html)

except Exception:
    print(Exception)

finally:
    driver.close()
    driver.quit()
with open('youla.html', 'r', encoding='utf-8') as file:
    request = file.read()

soup = BeautifulSoup(request, 'lxml')
all_links = soup.find_all('div', class_='sc-llGDqb sc-gqgnwQ fEAASo hZGRky')
links = []
for lin in all_links:
    link = lin.find('a').get('href')
    links.append({
        'link': link
    })


with open('test.json', 'w', encoding='utf-8') as file:
    json.dump(links, file, indent=4, ensure_ascii=False)

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

<span class="sc-llGDqb sc-gqgnwQ fEAASo hZGRky"><a href="/moskva/ehlektronika/naushniki/airpods-pro-rieplika-62375dadef55301e84039b4c" title="AirPods Pro реплика" target="_blank" rel="noopener noreferrer" style="cursor: pointer;"><figure data-test-component="ProductCard" data-test-id="62375dadef55301e84039b4c" class="sc-hsZwpi fssEAr"><div class="sc-gcHwEF sc-fkxeQW eHsIOL jskOCU"><div class="sc-mxuny kRlvNr"><svg class="sc-dOrDzm uyVta sc-hJMuen ipuCdo" role="img" aria-label="">

Ответы

▲ 0

Вам необходимо заменить название тега для поиска c div на span :

all_links = soup.find_all('span', class_='sc-llGDqb sc-gqgnwQ fEAASo hZGRky')