How can I use the time
module to skip to the next iteration if it's taking longer than 5 seconds? Specifically what is the correct way to implement the solution from How would I stop a while loop after n amount of time? with the for
loop I'm using below?
import requests
from bs4 import BeautifulSoup
for n in random_list:
url = all_raw_urls[n]
try:
req = requests.get(url)
data = req.text
soup = BeautifulSoup(data, 'html.parser')
tags = soup.find_all('img')
tags = list(set(tags))
if len(tags) < 15 or len(tags) > 50:
print(str(image_count) + ': leave' + ' : images: ' + str(len(tags)))
else:
print(str(image_count) + ': keep' ' : images: ' + str(len(tags)))
image_urls.append(url)
except:
print('request error')
image_count += 1