python – Many lines are parsed, but only one is written to json

I want to parse OLX (Ebay only in the CIS) and write the code to json, but when parsing, it only writes one line to me, I assume that I did not correctly place the entry in the dictionary. Help me please.

main

import lxml
from bs4 import BeautifulSoup
import json
import requests


request = input(f'Put requqest: ')
page = int(input('Page: '))
currency = input('UAH/EUR/USD: ')


headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
    'accept': '*/*'
}


notebooks = {}
for x in range(1, page):

    url = f'https://www.olx.ua/d/list/q-{request}/?currency={currency}&page={page}'
    req = requests.get(url=url, headers=headers)
    soup = BeautifulSoup(req.text, 'lxml')
    all_ads_link = soup.find_all(class_='css-1bbgabe')
    all_ads_names = soup.find_all(class_='css-v3vynn-Text')


    for name in all_ads_names:
        namea = name.text

    for ad in all_ads_link:
        url_ad = ad.get('href')
        notebooks[namea] = 'https://www.olx.ua'+url_ad

with open(f'{page}_{request}', 'a', encoding='utf-8') as file:
    json.dump(notebooks, file, indent=4, ensure_ascii=False)


<div data-cy="l-card" class="css-19ucd76"><a class="css-1bbgabe" href="/d/obyavlenie/prodam-proektor-IDOQ9oO.html"><div class="css-qfzx1y"><div type="list" class="css-1venxj6"><div type="list" class="css-1ut25fa"><div type="list" class="css-pn1izb"><div class="css-gl6djm"><img src="https://ireland.apollo.olxcdn.com:443/v1/files/16qhfuk4icmd1-UA/image;s=200x0;q=50" srcset="https://ireland.apollo.olxcdn.com:443/v1/files/16qhfuk4icmd1-UA/image;s=100x0;q=50 100w,
        https://ireland.apollo.olxcdn.com:443/v1/files/16qhfuk4icmd1-UA/image;s=200x0;q=50 200w,
        https://ireland.apollo.olxcdn.com:443/v1/files/16qhfuk4icmd1-UA/image;s=300x0;q=50 300w,
        https://ireland.apollo.olxcdn.com:443/v1/files/16qhfuk4icmd1-UA/image;s=400x0;q=50 400w,
        https://ireland.apollo.olxcdn.com:443/v1/files/16qhfuk4icmd1-UA/image;s=600x0;q=50 600w" sizes="150px" alt="Продам проектор !" class="css-gwhqbt"></div></div><div class="css-13aawz3"><div class="css-1av34ht"><div class="css-3xiokn"><div data-testid="adCard-featured" class="css-1katuj6" height="24" font-size="12" font-weight="bold" letter-spacing="0.8" color="background-global-primary">ТОП</div><div class="css-1xwefxo"><div class="css-10arydl"><svg width="1em" height="1em" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="css-d7r8uj"><path d="M21 15.999h-.343A3.501 3.501 0 0 0 17.5 14a3.501 3.501 0 0 0-3.156 1.997l-4.687.002A3.5 3.5 0 0 0 6.5 14a3.5 3.5 0 0 0-3.158 2L3 16.002V5h11v6l1 1h6v3.999zM17.5 19c-.827 0-1.5-.673-1.5-1.5s.673-1.5 1.5-1.5 1.5.673 1.5 1.5-.673 1.5-1.5 1.5zm-11 0c-.827 0-1.5-.673-1.5-1.5S5.673 16 6.5 16s1.5.673 1.5 1.5S7.327 19 6.5 19zm12-12 2.25 3H16V7h2.5zm1-2H16V4l-1-1H2L1 4v13.002l1.001 1 1.039-.001A3.503 3.503 0 0 0 6.5 21a3.502 3.502 0 0 0 3.46-3l4.08-.003A3.503 3.503 0 0 0 17.5 21a3.502 3.502 0 0 0 3.46-3.001H22l1-1V9.665L19.5 5z" fill="currentColor" fill-rule="evenodd"></path></svg></div></div></div></div></div></div><div type="list" class="css-9nzgu8"><div class="css-u2ayx9"><h6 class="css-v3vynn-Text eu5v0x0">Продам проектор !</h6><p data-testid="ad-price" class="css-wpfvmn-Text eu5v0x0"><div class="css-1c0ed4l"><svg width="1em" height="1em" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="css-1ojrdd5"><path d="M21 15.999h-.343A3.501 3.501 0 0 0 17.5 14a3.501 3.501 0 0 0-3.156 1.997l-4.687.002A3.5 3.5 0 0 0 6.5 14a3.5 3.5 0 0 0-3.158 2L3 16.002V5h11v6l1 1h6v3.999zM17.5 19c-.827 0-1.5-.673-1.5-1.5s.673-1.5 1.5-1.5 1.5.673 1.5 1.5-.673 1.5-1.5 1.5zm-11 0c-.827 0-1.5-.673-1.5-1.5S5.673 16 6.5 16s1.5.673 1.5 1.5S7.327 19 6.5 19zm12-12 2.25 3H16V7h2.5zm1-2H16V4l-1-1H2L1 4v13.002l1.001 1 1.039-.001A3.503 3.503 0 0 0 6.5 21a3.502 3.502 0 0 0 3.46-3l4.08-.003A3.503 3.503 0 0 0 17.5 21a3.502 3.502 0 0 0 3.46-3.001H22l1-1V9.665L19.5 5z" fill="currentColor" fill-rule="evenodd"></path></svg></div>96 €<span class="css-e2218f">Договорная</span></p></div><div><p data-testid="location-date" class="css-p6wsjo-Text eu5v0x0">Вишневое - 15 июня 2022 г.</p><p class="css-1bhbxl1-Text eu5v0x0"></p></div><span data-testid="adAddToFavorites" class="css-1x8zoa0"><div class="css-1fxp90q"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="1em" height="1em" class="css-znbvx0"><path fill="currentColor" fill-rule="evenodd" d="M20.219 10.367 12 20.419 3.806 10.4A3.96 3.96 0 0 1 3 8c0-2.206 1.795-4 4-4a4.004 4.004 0 0 1 3.868 3h2.264A4.003 4.003 0 0 1 17 4c2.206 0 4 1.794 4 4 0 .868-.279 1.698-.781 2.367M17 2a5.999 5.999 0 0 0-5 2.686A5.999 5.999 0 0 0 7 2C3.692 2 1 4.691 1 8a5.97 5.97 0 0 0 1.232 3.633L10.71 22h2.582l8.501-10.399A5.943 5.943 0 0 0 23 8c0-3.309-2.692-6-6-6"></path></svg><div data-testid="favorite-icon" class="css-5xgpg7">Подписаться</div></div></span></div></div></div></a></div>

json

{
    "Телефон стационарный кнопочный": "https://www.olx.ua/d/obyavlenie/telefon-statsionarnyy-knopochnyy-IDMMDY3.html"
}

terminal

Put requqest: Телефон
Page: 3
UAH/EUR/USD: UAH

Process finished with exit code 0

Leave a Comment