pythonimageweb-scrapingimagedownload

How to set name for download images


I think I need help with for loop. I have a list of 33 images. For each image I need to create a name: name, reference and number. Every product with the same reference number has to ends with number from 1 to ...x

*Like her:

enter image description here

Can someone give me a hint how to finish the code? Now the products with same reference number are overwritten in the output (filenames).

import requests
from bs4 import BeautifulSoup


productlinks = []

for x in range(1, 2):
    r = requests.get(
        f'https://www.roco.cc/ren/products/locomotives/steam-locomotives.html?p={x}&verfuegbarkeit_status=41%2C42%2C43%2C45%2C44')
    soup = BeautifulSoup(r.content, 'lxml')
    productlist = soup.find_all('li', class_='item product product-item')

    for item in productlist:
        for link in item.find_all('a', class_='product-item-link', href=True):
            productlinks.append(link['href'])


wayslist = []

for url in productlinks :
   r = requests.get(url, allow_redirects=False)
   soup = BeautifulSoup(r.content, 'html.parser')
   images = soup.findAll('img')
   for image in images:
      if 'def' in image['src']:

        name = 'Roco'

        try:
            reference = soup.find(
                'span', class_='product-head-artNr').get_text().strip()
        except Exception as e:
            print(link)

        ways = image['src']
        
        wayslist.append(ways)

        with open(name + '_' + reference + '_' + '.jpg', 'wb') as f:
                im = requests.get(ways)
                f.write(im.content)
                print('Writing: ', ways)
        
       
print(len(wayslist))

Solution

  • You can use enumerate:

    images = soup.findAll('img')
    
    counter = 1
    
    for image in images:
        if 'def' in image['src']:
    
            name = 'Roco'
    
            try:
                reference = soup.find(
                    'span', class_='product-head-artNr').get_text().strip()
            except Exception as e:
                print(link)
    
            ways = image['src']
        
            wayslist.append(ways)
    
            with open(name + '_' + reference + '_' + str(counter) + '.jpg', 'wb') as f:
                im = requests.get(ways)
                f.write(im.content)
                print('Writing: ', ways)
    
            counter += 1
    

    UPDATE:

    In your case, if you have additional condition inside for statement, it's better to have outer counter and increase it every loop you need manually. Please note that you don't need enumerate in this example. I've updated original code.