1

Every morning, Multnomah County posts whether or not it's OK to light a fire in your fireplace. Instead of heading to the site everyday, I'm trying to automate the process with an app that grabs an image from the server that shows the status.

Right now, the script successfully gives me the link to the image, which (today) is https://multco.us/sites/default/files/styles/small/public/Copy%20of%20Advisory%20-%20Green.jpg.

I've been searching and searching and can't find a way to actually download the image or grab it so I can send it to myself via a Twilio SMS app.

Here's my script:

from bs4 import BeautifulSoup
import requests

source = requests.get(
    'https://multco.us/health/staying-healthy/winter-wood-burning-restrictions').text

soup = BeautifulSoup(source, 'lxml')

img = soup.find('div', class_='field-body').h2.img.attrs['src']
img_url_split = img.split('?')
img_url = img_url_split[0]

print(img_url)

2 Answers2

0

You could install wget with pip install wget and do something like that:

import wget

url = "https://multco.us/sites/default/files/styles/small/public/Copy%20of%20Advisory%20-%20Green.jpg"
filename = wget.download(url)
kir0ul
  • 59
  • 2
  • 4
0

Try making another requests.get() to the image URL, and saving the content to a file

from bs4 import BeautifulSoup
import requests

URL = "https://multco.us/health/staying-healthy/winter-wood-burning-restrictions"

soup = BeautifulSoup(requests.get(URL).content, "lxml")

img_link = soup.find("div", class_="field-body").h2.img.attrs["src"].split("?")[0]

img = requests.get(img_link)

with open("image.jpg", "wb") as f:
    f.write(img.content)
MendelG
  • 14,885
  • 4
  • 25
  • 52