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

我只是一个虾纸丫 提交于 2021-02-05 12:31:07

问题


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 advance.

def scrapeurls():
    domain = "https://domain234dd.com"
    count = 0

    while count < 10:

        page = requests.get("{}{}".format(domain, count))
        soup = BeautifulSoup(page.content, 'html.parser')
        data = soup.findAll('div', attrs={'class': 'video'})

        urls = []

        for div in data:
            links = div.findAll('a')
            for a in links:
                urls.append(a['href'])
                print(a['href'])

        print(count)
        count += 1

回答1:


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




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/46565752/python-previous-list-elements-being-overwritten-by-new-elements-during-while-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!