Working with Profile Pictures
img src=”data”: how to read and save off a photo
This past week, I worked on some Computer Vision items, and I came across a new arrangement. <img src=”data:image/png;base64,..>. Generally I was used to seeing <img src=”https….”>. After a little research, I understood this well and wanted to share what I learned with you. Naturally, I first tried to request the image URL, but that really didn’t work out. Let’s take a look at what I learned.
First, we pull in our Python modules.
import time
from selenium import webdriver
import requests
import shutilfrom bs4 import BeautifulSoup
from selenium import webdriver
from base64 import decodestring, decodebytes
import os
from urllib.parse import urlparse
We need Selenium and BeautifulSoup as the main workhorses and base64 to decode that <img scr=’data:image/png..’>. You can see that I had requests and urlparse, but those won’t work for me and certainly will not work for you. Next we need to grab the page which contains the image(s) we are working on.
driver = webdriver.Chrome('/Applications/chromedriver')
driver.get(profile);
time.sleep(5) # Let the user actually see something!
soup = BeautifulSoup(driver.page_source, 'lxml')
picture = soup.find('div','user-photo-inner')
pic_url = picture.img['src']
a =…