Grabbing scope shots has always been a pain in the ass. First it was the shitty Tektronix scopes my alma mater used: you needed a floppy disk to get shots. (Read that again. Floppy drive. FUCK.) Then it was slightly less shitty scopes at an internship that were USB stick enabled. Better. Not great, but better. Now, I'm employed, and got my boss to cash out for one of these beauties. (Scope, how do I love thee; let me count the ways...) It has the perk of being web enabled, but it's still a hassle to get the scope shot.
Navigate to a webpage? Click a button? Right click and "Save Target As"?! You've got to be fucking kidding me.
There is a better way. Guido made Python so lazy people's lives are easy. You can run this as a standalone script to save your .png in the current working directory; if you run it from iPython, like I do, you'll have to %pwd to find where your notebook lives if you don't know already.
import requests
from bs4 import BeautifulSoup
BASE_URL = 'http://10.20.20.91' # This is the IP Address of my scope
CAPTURE = '/getImage.asp?inv=false' # This is the URI of the image capture script
r = requests.get(BASE_URL+CAPTURE, stream=True) # Call it this way to get the latest image
for img in BeautifulSoup(r.content).find_all('img'):
f_name = img['src'].split('/')[-1] # chop down img['src'] to the filename w/o the path
with open(f_name, 'wb') as f:
f.write(requests.get(BASE_URL + img['src']).content) # open, read, write, beer
It would be slick if you didn't need to import both requests and urllib2. I'm sure there's a way to do it with just requests, but couldn't find a decent how-to. Feel free to link me if you know where one is!
UPDATE @ 9:13AM, March 28 Thanks to stunsch for pointing out how to do this using just requests - no urllib2 necessary. Nice spot!
What's wrong with this?