python爬虫指定关键字抓图片(能直接用的代码)
时间: 2024-05-01 18:18:31 浏览: 78
以下是一个简单的 Python 爬虫代码,可以利用关键字(例如“dog”)从 Google Images 上抓取图片:
```python
import requests
from urllib.parse import urlparse, parse_qs
from bs4 import BeautifulSoup
from io import BytesIO
from PIL import Image
keyword = "dog"
url = f"https://www.google.com/search?q={keyword}&tbm=isch"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
images = soup.find_all("img")
for img in images:
img_url = img.attrs.get("src")
if not img_url:
img_url = img.attrs.get("data-src")
if not img_url:
continue
if "http" not in img_url:
img_url = f"https:{img_url}"
try:
image_content = requests.get(img_url).content
except Exception as e:
print(f"ERROR - Could not download {img_url} - {e}")
continue
try:
image_file = BytesIO(image_content)
image = Image.open(image_file).convert('RGB')
file_name = urlparse(img_url).path.split("/")[-1]
with open(file_name, "wb") as f:
image.save(f, "JPEG", quality=85)
except Exception as e:
print(f"ERROR - Could not save {img_url} - {e}")
continue
```
阅读全文