---
# System prepended metadata

title: Fetch Image

---

# Fetch Image


```python
import re
import requests
from bs4 import BeautifulSoup

#keyword
keyword = "水獺"

# URL
url = "https://www.google.com/search?q=" + keyword + "&safe=active&source=lnms&tbm=isch"

# Get request
response = requests.get(url)

# Parsering Response
soup = BeautifulSoup(response.text, "html.parser")

# find out image file 
images = soup.find_all("img")

# craete the list to save the image 
imageList = []


# regular expression
pattern = "^https"




for image in images:



  # get the image src 
  imageURL = image.get("src")

  # Regular
  if re.match(pattern, imageURL):
     imageList.append(imageURL)

print(imageList)

# -------------------------------------------------------------


# Download the image 

imageNumber = 0


for url in imageList:

    response = requests.get(url)
    with open(f"{keyword} {imageNumber}.jpg","wb") as file:
        file.write(response.content) 
    
    imageNumber += 1
```


Result 

![](https://i.imgur.com/0z1VfQi.png)

![](https://i.imgur.com/DOqCr80.png)

Author: 侯智晟
Gmail: meowheckerouo@gmail.com