Search code examples
pythonbeautifulsoupurllib2urllib

Downloading Image with no Extension


I have searched a lot but haven't found a good answer on how to use BeautifulSoup and urllib to use a src link and download the image when it is without an extension, for example, the image for the Facebook app icon @ https://play.google.com/store/apps/details?id=com.facebook.katana is https://lh3.googleusercontent.com/ZZPdzvlpK9r_Df9C3M7j1rNRi7hhHRvPhlklJ3lfi5jk86Jd1s0Y5wcQ1QgbVaAP5Q=w300-rw - without an extension.

What library or method should I be using to download such images?


Solution

  • You can get the image using the img tag using the class name and the src attribute:

    import requests
    import urllib
    r = requests.get("https://play.google.com/store/apps/details?id=com.facebook.katana")
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(r.content)
    
    img = soup.find("img",{"class":"cover-image"})["src"]
    urllib.urlretrieve(img,"fb.jpg")