Search code examples
pythonbeautifulsoupurlretrieve

How can I use a for loop to modify a variable inside a string while I download a list of images using BS?


I am using BeautifulSoup to scrape a page with a table. When I am going to download some images inside that table using a for loop and "urlretrieve", I am not able to give each image a different name, thus the each time an image is downloaded, it is replaced by a new image because they have the same name.

In other words, I am not able to change a variable inside a string so that I can give each downloaded image a different name.

enter image description here

enter image description here

enter image description here


Solution

  • From your last picture, you are setting num to 0 at each loop iteration. You want to take it out of the loop.

    for ...
      num=0
    

    to

    num = 0
    for ...
      num += 1