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:
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,42,43,45,44')
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))
CodePudding user response:
You can use enumerate
:
images = soup.findAll('img')
for i, image in enumerate(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(i 1) '.jpg', 'wb') as f:
im = requests.get(ways)
f.write(im.content)
print('Writing: ', ways)