Search code examples
pythonbeautifulsoupfindall

Beautiful Soup findAll() finds half of them


I'm trying to scrape information on the price of offices in France and I successfully developed the code to scrape all the information I needed.

Though, I quickly noticed that something was wrong with the number of outputs and more precisely that my algorithm was returning only half of the occurences present on each page of the website.

Here's how the basic code looks like:

from urllib.request import Request, urlopen
from bs4 import BeautifulSoup

page = "https://www.bureauxlocaux.com/recherche/?transaction_type=sale&realty_type=office&page=1"
req = Request(page, headers={'User-Agent': 'Mozilla/5.0'})
web_byte = urlopen(req).read()
webpage = web_byte.decode('utf-8')
bs = BeautifulSoup(webpage, 'html.parser')
announces = bs.findAll('li',{'class':'item-card'})

Like suggested here Beautiful Soup findAll doesn't find them all I'm already using the html.parser and I've tried with others but in vain.

I still don't understand why it's picking up only the first half of the page whereas the html code clearly comprises all of them.


Solution

  • The data you see on the page is stored as Json. You can use json module to extract it.

    For example:

    import json
    import requests
    from bs4 import BeautifulSoup
    
    
    url = 'https://www.bureauxlocaux.com/recherche/?transaction_type=sale&realty_type=office&page=1'
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    data = json.loads(soup.select_one('#listings-data').contents[0])
    
    # uncomment this to print all data:
    # print(json.dumps(data, indent=4))
    
    for idx, i in enumerate(data['results']['items'], 1):
        if i.get('sponsored_page'):
            continue
        print('{:<3} {:<50} {:<30} {:<30} {}'.format(idx, i['label'], i['street'], i['city'], i['zip_code']))
    

    Prints:

    1   A VENDRE, LE RIVE DE SAÔNE                         41 QUAI FULCHIRON              Lyon                           69005
    2   A VENDRE BUREAUX 84 m² RDC CHAMPS SUR MARNE        12 rue Albert Einstein         Champs sur Marne               77420
    3   Vente Bureau Boulogne Billancourt 92100            RUE D'ISSY                     Boulogne Billancourt           92100
    4   A VENDRE Bâtiment indépendant                                                     TOUSSUS LE NOBLE               78117
    5   Exclusivité Yfimo Buchelay à vendre un bureau de 85 m2 20 rue des Piquettes           BUCHELAY                       78200
    6   A VENDRE OU A LOUER, EUROPARC DE BRON              6 RUE EDISON                   Bron                           69500
    7   A VENDRE, LE RIVE DE SAÔNE                         41 QUAI FULCHIRON              Lyon                           69005
    8   A VENDRE, LE RIVE DE SAÔNE                         41 QUAI FULCHIRON              Lyon                           69005
    9   A VENDRE OU A LOUER, EUROPARC DE BRON              6 RUE EDISON                   Bron                           69500
    10  Quartier Palais de justice : Bureaux à vendre                                     MARSEILLE                      13006
    11  BUREAUX A VENDRE                                                                  Toulouse                       31100
    12  A VENDRE OU A LOUER                                5 RUE JEAN MERMOZ              Courcouronnes                  91080
    13  BUREAUX A VENDRE                                   12, place GEORGES POMPIDOU     Noisy Le Grand                 93160
    14  A VENDRE OU A LOUER, EUROPARC DE BRON              6 RUE EDISON                   Bron                           69500
    15  BUREAUX A VENDRE OU A LOUER                                                       Toulouse                       31100
    16  NANTES A VENDRE BUREAU 76 M² ANCIEN DE CARACTERE                                  Nantes                         44100
    17  Vente Bureau Marseille 13010                       AVENUE DE LA CAPELETTE         Marseille                      13010
    18  A VENDRE, LE RIVE DE SAÔNE                         41 QUAI FULCHIRON              Lyon                           69005
    19  Bureaux à vendre - 13016 Marseille                 7 avenue André Roussin         MARSEILLE                      13016
    20  A VENDRE OU A LOUER, EUROPARC DE BRON              6 RUE EDISON                   Bron                           69500
    21  BUREAUX A VENDRE                                                                  Toulouse                       31100
    22  A VENDRE OU A LOUER, EUROPARC DE BRON              6 RUE EDISON                   Bron                           69500
    24  BUREAUX A VENDRE OU A LOUER                                                       Lissieu                        69380
    25  BUREAUX A VENDRE OU A LOUER                                                       Lissieu                        69380
    26  A VENDRE OU A LOUER                                350 AVENUE GUILIBERT DE LA LAUZIERE Aix En Provence                13100
    27  Location Bureau MULHOUSE 68100                     RUE FRANCOIS SPOERRY           MULHOUSE                       68100
    28  Vente Bureau LYON 69009                                                           LYON                           69009
    29  Bureau de 60m²                                                                    Champs Sur Marne               77420
    30  Location Bureau FRANCHEVILLE 69340                 1 BIS Chemin DU TOREY          FRANCHEVILLE                   69340
    31  Vente Bureau Massy 91300                           1, rue Marcel Paul             Massy                          91300
    32  Bureau de 159m²                                                                   Saint Maur Des Fosses          94100
    33  Bureaux à vendre de 280m2                          RUE FOUCHER LEPELLETIER        Issy Les Moulineaux            92130
    34  BUREAUX A VENDRE                                   12, allée Lech Walesa          Lognes                         77185
    35  Vente Bureau BORDEAUX 33000                        4 Rue EMILE FOURCAND           BORDEAUX                       33000
    36  Location Bureau RENNES 35700                       158 Rue 158 Rue d'Antrain      RENNES                         35700
    37  Vente Bureau BRUGES 33520                          Rue SERGE DEJEAN               BRUGES                         33520
    38  A VENDRE OU A LOUER                                                               Marseille                      13009
    39  BUREAUX A VENDRE OU A LOUER                                                       Bussy Saint Georges            77600
    40  A vendre bureaux occupés proche Opéra                                             Paris                          75009
    41  Vente Bureau Saint Ouen 93400                                                     Saint Ouen                     93400
    42  Rénovés - double parking - face Gare RER           22 square Robinson             SCEAUX                         92330
    43  Rare – Madeleine – 6 pièces – Beaux volumes        rue de Surène                  PARIS                          75008
    44  BUREAUX A VENDRE OU A LOUER                                                       Bussy Saint Georges            77600
    45  Vente Bureau Nanterre 92000                                                       Nanterre                       92000
    46  Vente Bureau Noisy Le Grand 93160                                                 Noisy Le Grand                 93160
    47  Vente Bureau Merignac 33700                                                       Merignac                       33700
    48  BUREAUX A VENDRE                                                                  Noisy Le Grand                 93160
    49  Vente Bureau Paris 75013                                                          Paris                          75013
    50  BUREAUX A VENDRE                                                                  Bordeaux                       33000
    51  Bureaux à louer et à vendre de 400m2               PLACE DU SUD                   Puteaux                        92800
    52  Vente Bureau Les Pavillons Sous Bois 93320                                        Les Pavillons Sous Bois        93320
    53  En Exclusivité - A Vendre - Bureaux - 186 m2 -  ZI Les Paluds - Agora                                Aubagne                        13400
    54  Vente bureaux Issy-les-moulineaux 92130                                           Issy Les Moulineaux            92130
    55  PARC D'ACTIVITE - ACTISQUARE III (bis)                                            Dommartin                      69380
    56  BUREAUX A VENDRE                                                                  Labege                         31670
    57  BUREAUX A VENDRE OU A LOUER                                                       Bussy Saint Georges            77600
    58  BUREAUX A VENDRE                                                                  Noisy Le Grand                 93160
    59  BUREAUX D'ENVIRON 370 m² DISPONIBLES IMMEDIATEMENT A l'ACHAT A FONDETTES                                Fondettes                      37230
    60  A VENDRE OU A LOUER                                                               Asnieres Sur Seine             92600
    61  A VENDRE - BUREAUX DE 130 M² IDÉALEMENT SITUÉS EN FACE DE L’AÉROPORT                                Blagnac                        31700
    62  A VENDRE OU A LOUER                                                               Villefranche Sur Saone         69400
    63  Vente Bureau PARIS 75015                           8 RUE BORROMEE                 PARIS                          75015
    64  La Joliette/Bureaux à vendre en pied d'immeuble                                   Marseille                      13002
    65  Vente Bureau Asnieres Sur Seine 92600                                             Asnieres Sur Seine             92600
    66  Vente Bureau OULLINS 69600                         27 Rue Saint-Exupéry           OULLINS                        69600
    67  Bureaux A VENDRE                                   Allée de la connaissance       LIEUSAINT                      77127
    68  A VENDRE OU A LOUER                                                               Chateau Gaillard               01500
    69  BUREAUX A VENDRE OU A LOUER                                                       Lissieu                        69380
    70  Vente Bureau CESSON SEVIGNE 35510                                                 CESSON SEVIGNE                 35510
    71  IMMEUBLE DE BUREAUX PRIVATIF A VENDRE                                             Merignac                       33700
    72  IMMEUBLE EMBLEMATIQUE CENTRE-VILLE DE BOULOGNE-SUR-MER Place des Capucins             BOULOGNE SUR MER               62200
    73  Vente Bureau Paris 75008                                                          Paris                          75008
    74  A LOUER /  A VENDRE - QUIMPER - BUREAUX - 115 m² env                                Quimper                        29000
    75  Location Bureau BORDEAUX 33000                     Avenue DES 40 JOURNAUX         BORDEAUX                       33000
    76  Vente Bureau Courbevoie 92400                                                     Courbevoie                     92400
    77  BUREAUX A VENDRE OU A LOUER                                                       Lissieu                        69380
    78  Vente Bureau Asnieres Sur Seine 92600                                             Asnieres Sur Seine             92600
    79  Vente Bureau Asnieres Sur Seine 92600                                             Asnieres Sur Seine             92600
    80  Bureaux Vide 20 m²                                                                Cannes                         06400
    81  Vente Bureau ROSNY SOUS BOIS 93110                 14 RUE DE LISBONNE             ROSNY SOUS BOIS                93110
    82  Plateau de Bureaux à vendre / Palais Royal                                        paris                          75002
    83  A vendre bureaux 118m² centre ville Marseille                                     Marseille                      13006
    84  Vente Bureau Clichy 92110                          92-98 BOULEVARD VICTOR HUGO    Clichy                         92110
    85  BUREAUX 138 m² (non div.) à vendre (PARIS 75002)                                  Paris                          75002
    86  Vente Bureau Gennevilliers 92230                   AVENUE DES LOUVRESSES          Gennevilliers                  92230
    87  Location Bureau VILLENEUVE D ASCQ 59650            25 Rue DU CARROUSEL            VILLENEUVE D ASCQ              59650
    88  A VENDRE OU A LOUER                                Place des Champs Elysées       Courcouronnes                  91080
    89  BUREAUX A VENDRE                                                                  Echirolles                     38130
    90  A VENDRE OU A LOUER                                rue des Fossés 
    rue des Fossés Melun                          77000
    91  Vente Bureaux Paris 75011                                                         Paris                          75011