How to use Colab to scrape images from a website with Python? or Best way to scrape images from a web

16 Replies, 1352 Views

Hey! For colab scrape images from website python, I'd recommend using `requests` + `BeautifulSoup`. Here's a quick snippet that works in Colab:

```python
import requests
from bs4 import BeautifulSoup
import os

url = "your_target_website"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
images = soup.find_all('img')

for img in images:
img_url = img['src']
img_data = requests.get(img_url).content
with open(os.path.basename(img_url), 'wb') as f:
f.write(img_data)
```

For lazy loading, you might need Selenium. Colab has it preinstalled, so just `!apt install chromium-chromedriver` and you're good to go!

Messages In This Thread



Users browsing this thread: 1 Guest(s)