shaheerawan3 commited on
Commit
3538bec
·
verified ·
1 Parent(s): cb0f28d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -34,8 +34,10 @@ class ImageScraper:
34
  url = f"https://www.pexels.com/search/{query.replace(' ', '%20')}/"
35
  response = requests.get(url, headers=self.headers)
36
  soup = BeautifulSoup(response.text, 'html.parser')
37
- for img in soup.find_all('img', src=True):
38
- if 'photos' in img['src'] and 'pexels.com' in img['src']:
 
 
39
  urls.append(img['src'])
40
  except Exception as e:
41
  print(f"Pexels scraping error: {e}")
@@ -47,9 +49,12 @@ class ImageScraper:
47
  url = f"https://unsplash.com/s/photos/{query.replace(' ', '-')}"
48
  response = requests.get(url, headers=self.headers)
49
  soup = BeautifulSoup(response.text, 'html.parser')
50
- for img in soup.find_all('img', src=True):
51
- if 'images.unsplash.com' in img['src']:
52
- urls.append(img['src'])
 
 
 
53
  except Exception as e:
54
  print(f"Unsplash scraping error: {e}")
55
  return urls
 
34
  url = f"https://www.pexels.com/search/{query.replace(' ', '%20')}/"
35
  response = requests.get(url, headers=self.headers)
36
  soup = BeautifulSoup(response.text, 'html.parser')
37
+
38
+ # Updated selector to target image sources
39
+ for img in soup.find_all('img', {'data-image-width': True}):
40
+ if img.get('src') and 'photos' in img['src']:
41
  urls.append(img['src'])
42
  except Exception as e:
43
  print(f"Pexels scraping error: {e}")
 
49
  url = f"https://unsplash.com/s/photos/{query.replace(' ', '-')}"
50
  response = requests.get(url, headers=self.headers)
51
  soup = BeautifulSoup(response.text, 'html.parser')
52
+
53
+ # Updated selector for Unsplash
54
+ for img in soup.find_all('img', {'srcset': True}):
55
+ src = img.get('src')
56
+ if src and 'images.unsplash.com' in src:
57
+ urls.append(src)
58
  except Exception as e:
59
  print(f"Unsplash scraping error: {e}")
60
  return urls