How to web scrape images from HTML using BeautifulSoup (bs4)? or What's the best way to web scrape im

14 Replies, 1156 Views

Hey! I had the same issue when learning how to web scrape images from html bs4. Here's a quick snippet that might help:

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

url = 'your_url_here'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

for img in soup.find_all('img'):
img_url = img['src']
if not img_url.startswith('http'):
img_url = url + img_url # handle relative URLs
img_data = requests.get(img_url).content
with open(os.path.basename(img_url), 'wb') as f:
f.write(img_data)
```

For absolute vs relative URLs, you gotta check if the `src` starts with `http`. If not, prepend the base URL. Also, check out `urllib.parse` for cleaner URL handling!

Messages In This Thread



Users browsing this thread: 1 Guest(s)