Python - previous list elements being overwritten by new elements during while loop

前端 未结 3 589
庸人自扰
庸人自扰 2021-01-29 07:55

Hello I am new to Python and am trying to figure out why my list overwrites the previous elements every time a new page is loaded and scraped during the while loop. Thank you in

相关标签:
3条回答
  • 2021-01-29 08:18

    You need to initialize the URL list before the loop. If you initialize inside the loop it sets it back to nothing every time.

    0 讨论(0)
  • 2021-01-29 08:23

    Because you reset urls to an empty list in every iteration of the loop. You should move that to before the loop.

    (Note, the whole thing would be better expressed as a for loop.)

    0 讨论(0)
  • 2021-01-29 08:26
    domain = "https://domain234dd.com"
    count = 0
    
    urls = []
    while count < 10:
    
        page = requests.get("{}{}".format(domain, count))
        soup = BeautifulSoup(page.content, 'html.parser')
        data = soup.findAll('div', attrs={'class': 'video'})
    
        for div in data:
            links = div.findAll('a')
            for a in links:
                urls.append(a['href'])
                print(a['href'])
    
        print(count)
        count += 1
    
    0 讨论(0)
提交回复
热议问题