How to download the image using python

I have following xpath for YT comment owner’s thumbnail.

<a class="yt-simple-endpoint style-scope ytd-comment-renderer" href="/channel/UCX6OQ3DkcsbYNE6H8uQQuVA">
      <yt-img-shadow fit="" height="40" width="40" class="style-scope ytd-comment-renderer no-transition" loaded="" style="background-color: transparent;"><!--css-build:shady--><img id="img" draggable="false" class="style-scope yt-img-shadow" alt="MrBeast" height="40" width="40" src=""></yt-img-shadow>

I can get the url value of image from src as

Could anyone please tell me how to download this image using python ?

>Solution :

You can do it by using urlib.request package

Here, you can simply pass the image URL(from where you want to download and save the image) and directory(where you want to save the download image locally, and give the image name with .jpg or .png) Here I given "local-filename.jpg" replace with this.


import urllib.request
imgURL = ""

urllib.request.urlretrieve(imgURL, "D:/abc/image/local-filename.jpg")

Leave a Reply