goldsilvy
goldsilvy

Reputation: 41

Python - web scraping - pagination stuck at page 1, doesn't progress further

new to python, wrote a following code:

import bs4
from urllib.request import urlopen as Open
from urllib.request import Request
from bs4 import BeautifulSoup as soup

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"}
results = "https://www.otodom.pl/sprzedaz/mieszkanie/?nrAdsPerPage=72&search%5Border%5D=created_at_first%3Adesc&page=1"
req = Request(url=results, headers=headers) 
html = Open(req).read()

page_soup = soup(html, "html.parser")
total_pages = int(page_soup.find("div",{"class":"after-offers clearfix"}).find("ul",{"class":"pager"}).findAll("li")[4].text)

page_number = 0

if page_number < total_pages:
    page_number = page_number + 1
    results = "https://www.otodom.pl/sprzedaz/mieszkanie/?nrAdsPerPage=72&search%5Border%5D=created_at_first%3Adesc&page="+str(page_number)
    print(results)

    req = Request(url=results, headers=headers) 
    html = Open(req).read()

    page_soup = soup(html, "html.parser")

    listings = page_soup.findAll("article",{"data-featured-name":"listing_no_promo"})
    print(len(listings))

I would have expected the end result to be a stream of printed out links, and number of listings on the page, yet all I have is:

https://www.otodom.pl/sprzedaz/mieszkanie/?nrAdsPerPage=72&search%5Border%5D=created_at_first%3Adesc&page=1
72

Any help would be appreciated, many thanks in advance!

Upvotes: 0

Views: 358

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195428

In your script you don't have any loop to get page_soup from new page.

This script scrapes the total number of pages and then iterates over them, prints names of offers and it's link:

import requests
from bs4 import BeautifulSoup as soup

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"}
results = "https://www.otodom.pl/sprzedaz/mieszkanie/?nrAdsPerPage=72&search%5Border%5D=created_at_first%3Adesc&page={}"

with requests.session() as s:
    req = s.get(results.format(1), headers=headers)

    page_soup = soup(req.text, "html.parser")
    total_pages = int(page_soup.find("div",{"class":"after-offers clearfix"}).find("ul",{"class":"pager"}).findAll("li")[4].text)

    print(total_pages)

    cnt = 1
    for page in range(1, 10): # <--- change 10 to total_pages to scrape all pages
        req = s.get(results.format(page), headers=headers)
        page_soup = soup(req.text, "html.parser")
        for a in page_soup.select('h3 a[data-featured-name="listing_no_promo"]'):
            name, link = a.find_next('span', {'class':'offer-item-title'}).text, a['href']

            print('{:<4} {:<50} {}'.format(cnt, name, link))
            cnt += 1

Prints:

1645
1    Biuro Sprzedaży Mieszkań 2 Pokoje Bezpośrednio     https://www.otodom.pl/oferta/biuro-sprzedazy-mieszkan-2-pokoje-bezposrednio-ID43LEw.html#b3d6f6add3
2    Przestronne mieszkanie na nowej inwestycji - 2020  https://www.otodom.pl/oferta/przestronne-mieszkanie-na-nowej-inwestycji-2020-ID43LEt.html#b3d6f6add3
3    Kapitalny remont, Grabiszyńska, parking            https://www.otodom.pl/oferta/kapitalny-remont-grabiszynska-parking-ID43LE0.html#b3d6f6add3
4    Przestronne mieszkanie przy ulicy Żurawiej         https://www.otodom.pl/oferta/przestronne-mieszkanie-przy-ulicy-zurawiej-ID43LDZ.html#b3d6f6add3
5    Katowice Bezpośrednio 3 Pokoje                     https://www.otodom.pl/oferta/katowice-bezposrednio-3-pokoje-ID43LDX.html#b3d6f6add3
6    2 Pokojowe mieszkanie na osiedlu zamkniętym Łomian https://www.otodom.pl/oferta/2-pokojowe-mieszkanie-na-osiedlu-zamknietym-lomian-ID43LDV.html#b3d6f6add3
7    Słoneczne 3 pokojowe w doskonałej lokalizacji !    https://www.otodom.pl/oferta/sloneczne-3-pokojowe-w-doskonalej-lokalizacji-ID43LDS.html#b3d6f6add3
8    Inteligenty apartament Zajezdnia Wrzeszcz          https://www.otodom.pl/oferta/inteligenty-apartament-zajezdnia-wrzeszcz-ID43LDR.html#b3d6f6add3
9    Mieszkanie, 32,04 m², Szczecin                     https://www.otodom.pl/oferta/mieszkanie-32-04-m-szczecin-ID43LDN.html#b3d6f6add3
10   M-3 Teofilów Na Sprzedaż                           https://www.otodom.pl/oferta/m-3-teofilow-na-sprzedaz-ID43LDI.html#b3d6f6add3
11   2-Pokojowe Mieszkanie                              https://www.otodom.pl/oferta/2-pokojowe-mieszkanie-ID43LDH.html#b3d6f6add3
12   2 duże pokoje w centrum Gdańsk ul. Zakopiańska     https://www.otodom.pl/oferta/2-duze-pokoje-w-centrum-gdansk-ul-zakopianska-ID43LDE.html#b3d6f6add3
13   M2 na Zabobrzu III                                 https://www.otodom.pl/oferta/m2-na-zabobrzu-iii-ID43LDx.html#b3d6f6add3
14   Mieszkanie 2 pokojowe ,atrakcyjna cena             https://www.otodom.pl/oferta/mieszkanie-2-pokojowe-atrakcyjna-cena-ID43LDv.html#b3d6f6add3
15   M2 Centrum Miasta, I piętro                        https://www.otodom.pl/oferta/m2-centrum-miasta-i-pietro-ID43LDt.html#b3d6f6add3
16   Rodzinny 3 Pokojowy Apartament z Ogródkiem         https://www.otodom.pl/oferta/rodzinny-3-pokojowy-apartament-z-ogrodkiem-ID43LDr.html#b3d6f6add3
17   2 pokoje. Aneks kuchenny. 45,5 m. Balkon           https://www.otodom.pl/oferta/2-pokoje-aneks-kuchenny-45-5-m-balkon-ID43LDp.html#b3d6f6add3


... and so on.

Upvotes: 3

Related Questions