Skip to content

Instantly share code, notes, and snippets.

@moorthi07
Created June 15, 2025 18:02
Show Gist options
  • Save moorthi07/4ce80f34eacd3473570171543aa91672 to your computer and use it in GitHub Desktop.
Save moorthi07/4ce80f34eacd3473570171543aa91672 to your computer and use it in GitHub Desktop.
plugsocket scrapper
import os
import requests
from bs4 import BeautifulSoup
def bing_image_scraper(query, download_path='outlet_images', num_images=50):
headers = {"User-Agent": "Mozilla/5.0"}
query = '+'.join(query.split())
url = f"https://www.bing.com/images/search?q={query}&form=HDRSC2"
if not os.path.exists(download_path):
os.makedirs(download_path)
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
img_tags = soup.find_all("img")
count = 0
for img in img_tags:
if count >= num_images:
break
img_url = img.get("src") or img.get("data-src")
if img_url and img_url.startswith("http"):
try:
img_data = requests.get(img_url).content
filename = os.path.join(download_path, f"{query}_{count}.jpg")
with open(filename, 'wb') as f:
f.write(img_data)
count += 1
except Exception as e:
continue
print(f"Downloaded {count} images to {download_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment