MCPcopy Create free account
hub / github.com/geekcomputers/Python / get_image_urls

Function get_image_urls

Wikipdedia/practice_beautifulsoap.py:54–71  ·  view source on GitHub ↗
(query)

Source from the content-addressed store, hash-verified

52
53
54def get_image_urls(query):
55 try:
56 search_url = f"https://www.google.com/search?q={query}&tbm=isch"
57 image_response = requests.get(search_url)
58 image_response.raise_for_status()
59 image_soup = BeautifulSoup(image_response.content, "html.parser")
60
61 image_urls = []
62 for img in image_soup.find_all("img"):
63 image_url = img.get("src")
64 if image_url and image_url.startswith("http"):
65 image_urls.append(image_url)
66
67 return image_urls[0]
68
69 except requests.exceptions.RequestException as e:
70 print("Error fetching image URLs:", e)
71 return None

Callers

nothing calls this directly

Calls 2

getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected