首页 > 解决方案 > Python的beautifulsoup刮板在几页后停止正常工作

问题描述

我正在搜索我当地的公寓出租/购买网站,并将它们写入 excel 文件。网站上的广告数量约为 9500,但我的刮板在 1000 左右后停止正常工作,然后显然每页只刮掉一个添加。

我添加了

prink(link) 

到循环,所以它告诉我它当前正在处理哪个页面。

结果如下:

https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=34
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=34
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=34
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=34
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=34
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=34
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=35
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=36
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=37
https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?strona=38

在第 34 页之后,它每页只加载一个添加。

我尝试将范围更改为 50,100 / 100,150 等,但它的行为类似,在大约 25-30 页之后它的行为类似于上面。

from bs4 import BeautifulSoup
from requests import get
import pandas as pd
import itertools
import matplotlib.pyplot as plt

headers = ({'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'})

tytuly = []
lokalizacje = []
Ceny = []
Ceny_m = []
Powierzchnie = []
L_pokoi = []
Pietra = []
rok_budowy = []

strona = 0
numer = 0
for strona in range(0,50):
    strona +=1
    link = 'https://ogloszenia.trojmiasto.pl/nieruchomosci-rynek-pierwotny/?' + 'strona=' + str(strona)
    r = get(link, headers = headers)
    zupa = BeautifulSoup(r.text, 'html.parser')
    ogloszenia= zupa.find_all('div', class_="list__item")
    print(link)
    for ogl in ogloszenia:
        try:
            tytul = ogl.find_all('h2', class_ ="list__item__content__title")[0].text
        except IndexError:
            tytul = ''
        try:
            powierzchnia = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[0].text
        except IndexError:
            powierzchnia = ''
        try:    
            liczba_pok = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[1].text
        except IndexError:
            liczba_pok = ''
        try:
            pietro = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[2].text
        except IndexError:
            pietro = ''
        try:
            if pietro == '':
                rok = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[2].text
            else:
                rok = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[3].text 
        except IndexError:
            rok = ''    
        try:
            lokalizacja = ogl.find_all('p', class_ = "list__item__content__subtitle")[0].text
        except IndexError:
            lokalizacja = ''
        try:
            cena = ogl.find_all('p', class_ = "list__item__price__value")[0].text
        except IndexError:
            cena = ''
        try:
            cena_m = ogl.find_all('p', class_ = "list__item__details__info details--info--price")[0].text
        except IndexError:
            cena_m = ''

        print(link)
        sys.getsizeof(tytuly)
        tytuly.append(tytul)
        lokalizacje.append(lokalizacja)
        Ceny.append(cena)
        Ceny_m.append(cena_m) 
        Powierzchnie.append(powierzchnia)  
        Pietra.append(pietro)
        L_pokoi.append(liczba_pok)
        rok_budowy.append(rok)

        kolumny = ["Tytul","Lokalizacja","Cena","Cena za metr","Powierzchnia","Pietro","Liczba pokoi","Rok budowy"]
zrzut = pd.DataFrame({"Tytul": tytuly,
                     "Lokalizacja": lokalizacje,
                     "Cena": Ceny,
                     "Cena za metr": Ceny_m,
                     "Powierzchnia": Powierzchnie,
                     "Pietro": Pietra,
                     "Liczba pokoi": L_pokoi,
                     "Rok budowy": rok_budowy})[kolumny]



zrzut.to_excel('rynek_pierwotnyy.xls')

我的猜测是列表正在超载,这就是它表现得这样的原因。我认为也许在每个循环之后清除列表并导出到 excel 会有所帮助?但如果我这样做,我将不得不附加 excel 文件。

标签: pythonlistweb-scrapingbeautifulsoup

解决方案


我在 0-50 和 0-100 范围内尝试了您的代码,它没有您的问题。也许问题与互联网或页面加载有关。请尝试使用 time.sleep()。希望对您有所帮助。


推荐阅读