【问题标题】:Python - web scraping - pagination stuck at page 1, doesn't progress furtherPython - 网页抓取 - 分页卡在第 1 页,没有进一步进展
【发布时间】:2020-03-28 02:34:24
【问题描述】:

python新手,写了如下代码:

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))

我本来希望最终结果是打印出的链接流和页面上的列表数量,但我所拥有的只是:

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

任何帮助将不胜感激,在此先感谢!

【问题讨论】:

    标签: python web-scraping pagination


    【解决方案1】:

    在您的脚本中,您没有任何循环可以从新页面获取 page_soup

    此脚本会抓取页面总数,然后对其进行迭代,打印报价名称及其链接:

    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
    

    打印:

    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.
    

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 2022-01-20
      • 2021-09-14
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      相关资源
      最近更新 更多