Download images with requests-HTML

Viewed 1040

There are several questions/answers here about downloading images in Python using requests. However, I can't find anything about doing this with requests-HTML. (https://requests.readthedocs.io/projects/requests-html/en/latest/). Is it possible to download images just using requests-HTML please, or do I need another library as well?

2 Answers

As per I know request-html used scrape data, however you can do it, first download the content of the image, then write it as png|jpg|etc.. hope this will help you.

from requests_html import HTMLSession
session = HTMLSession()
response = session.get("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png")
with open('tmp.png', 'wb') as w: 
    w.write(response.content)

requests-HTML uses requests under the hood. So you can use the response similar to requests. Something like the following solution will work.

from requests_html import HTMLSession

image_url = "https://github.githubassets.com/images/modules/logos_page/Octocat.png"
session = HTMLSession()
r = session.get(image_url)

with open("Octocat.png", "wb") as fp:
    fp.write(r.content)
Related