Beautifulsoup只能产生结果

时间:2020-07-01 13:54:31

标签: python beautifulsoup

我在这里搜索了许多类似的问题,但是仍然困惑为什么我的代码仅产生1个结果,而不是每页至少15个结果。

for pagenumber in range (0,2):
        url = 'https://www.autowereld.nl/volkswagen/?mdl=volkswagen_golf|volkswagen_golf-alltrack|volkswagen_golf-cabriolet|volkswagen_golf-plus|volkswagen_golf-sportsvan|volkswagen_golf-variant&p='
        txt = requests.get(url + str(pagenumber))
        soup = BeautifulSoup(txt.text, 'html.parser')
        soup_table = soup.find('article', class_="item")

        for car in soup_table.findAll('a'):
                link = car.get('href')
                sub_url = 'https://www.autowereld.nl' + link
                print(sub_url)

1 个答案:

答案 0 :(得分:0)

您正在使用soup.find查找标签为“ article”且类别为“ item”的东西。从documentation中,soup.find仅找到一个标签实例,而您正在寻找一个标签实例。像

for pagenumber in range (0,2):
    url = 'https://www.autowereld.nl/volkswagen/?mdl=volkswagen_golf|volkswagen_golf-alltrack|volkswagen_golf-cabriolet|volkswagen_golf-plus|volkswagen_golf-sportsvan|volkswagen_golf-variant&p='
    txt = requests.get(url + str(pagenumber))
    soup = BeautifulSoup(txt.text, 'html.parser')
    soup_articles = soup.findAll('article', class_="item")

    for article in soup_articles
        for car in article.findAll('a'):
            link = car.get('href')
            sub_url = 'https://www.autowereld.nl' + link
            print(sub_url)

可能会工作。如果您使用的是bs4,我也建议您使用find_all而不是findAll,因为这些方法的混合大小写版本已被弃用,但这取决于您。