【问题标题】:Python BeautifulSoup not scraping multiple pagesPython BeautifulSoup 没有抓取多个页面
【发布时间】:2019-03-05 20:34:13
【问题描述】:

我正在尝试从格式为每页 15 个广告的网页中抓取,然后您单击下一页并获取接下来的 15 个广告。

由于某种原因,脚本只从一个页面抓取,而不会转到另一个页面。

这是我的脚本代码:

page_num = 10
curr_page = 1
i = 1
car_title, price_hrk, year_made, km_made, date_pub, temp = [], [], [], [], [], []
title = soup.find_all(class_="classified-title")
price_kn = soup.find_all(class_="price-kn")
info = soup.find_all(class_="info-wrapper")
date = soup.find_all("span", class_="date")


# while the current page is less then or equal to the page_num variable
while curr_page <= page_num:
    # make a request with a current page
    page = requests.get("https://www.oglasnik.hr/prodaja-automobila?page={}".format(curr_page))
    # pass it to beautiful soup
    soup = BeautifulSoup(page.content, "html.parser")

    # while i is less then 15 elements on the single site
    while i <= 15:
        # check for existance
        if title[i]:
            # append the value
            car_title.append(title[i].get_text())
        else:
            # append NaN
            car_title.append(np.nan)

        if price_kn[i]:
            price_hrk.append(price_kn[i].get_text())
        else:
            price_hrk.append(np.nan)

        if date[i]:
            date_pub.append(date[i].get_text())
        else:
            date_pub.append(np.nan)

        # dual values, so append both to a temporary list
        for tag in info[i].find_all("span", class_="classified-param-value"):
            for val in tag:
                temp.append(val)

        try:
            # if length of element is less then 5
            if len(temp[0]) < 5:
                # it's a year, append to year_made list
                year_made.append(temp[0])
            km_made.append(temp[2])
        except IndexError:
            # if index out of bound append NaN
            year_made.append(np.nan)
            km_made.append(np.nan)

        # reset temp
        temp = []
        # add 1 to i element
        i += 1

    # add 1 to current page
    curr_page += 1

现在,如果我打印出其中一个列表的长度,我得到 15。

谁能告诉我我做错了什么或指出我正确的方向?

谢谢。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    您还需要重置您的i。就在之前(或之后)

       curr_page += 1
    

    添加:

       i = 1
    

    它应该可以工作。

    【讨论】:

    • 好的,这得到了数组中的 150 个项目,但由于某种原因,相同的 15 个项目重复了 10 次......
    • 这很奇怪;我在 Jupyter 笔记本中再次尝试,但每页仅显示 15 个项目。
    猜你喜欢
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多