Python Forum

Full Version: Get Webcam Image from URL - Octoprint
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone
I am currently writing a script to output the current progress from my 3D printer. I use Octoprint to control the printer remotely. I want to take a picture of the webcam and wanted to access the URL of the webcam. The page is structured as follows:
Output:
<html> <head> <meta name="viewport" content="width=device-width, minimum-scale=0.1"><title>webcam (640×480)</title> </head> <body style="margin: 0px; background: #0e0e0e;"> <img style="-webkit-user-select: none;" src="http://octopi/webcam/?action=snapshot"> </body> </html>
My question is, how can I download the img element? I already tried google and only found solutions with absolute URLS.
Does anyone have an idea where I can start or what I could look at?

Thank you and greetings
Konff
You parse out image url,the natural way with Python can be using BeautifulSoup and Requests.
A demo,and i use a image url that work to show download.
from bs4 import BeautifulSoup
import requests

html = '''\
<html>
    <head>
    <meta name="viewport" content="width=device-width, minimum-scale=0.1"><title>webcam (640×480)</title>
    </head>
    <body style="margin: 0px; background: #0e0e0e;">
        <img style="-webkit-user-select: none;" src="https://franceracing.fr/wp-content/uploads/2017/12/McLaren-Copier.jpeg">
    </body>
</html>'''

soup = BeautifulSoup(html, 'html.parser')
img_url = soup.find('img').get('src')
img = requests.get(img_url)
with open('McLaren.jpeg', 'wb') as f:
    f.write(img.content)
Thanks @snippsat it's works !