Created
June 7, 2022 23:34
-
-
Save derme302/3e4cfda75747a685efa45b59cfd43257 to your computer and use it in GitHub Desktop.
Download wallpaper from Bing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from requests.exceptions import RequestException | |
from lxml import etree | |
from io import BytesIO | |
BING_URL = "https://www.bing.com/" | |
def bing_get_wallpaper_url(debug = True): | |
session = requests.Session() | |
try: | |
bing_response = session.get(BING_URL) | |
if (debug): | |
print(bing_response.headers) | |
phaser = etree.HTMLParser() | |
dom_html = etree.parse(BytesIO(bing_response.content), phaser) | |
wallpaper_link = dom_html.xpath('//link[@id="preloadBg"]')[0] | |
wallpaper_url = wallpaper_link.attrib['href'] | |
if (debug): | |
print(etree.tostring(wallpaper_link, pretty_print=True, method="html")) | |
print(wallpaper_url) | |
return wallpaper_url | |
except RequestException as e: | |
print("No response from Bing: ", e) | |
if __name__ == "__main__": | |
print("Getting Wallpaper Link...") | |
wallpaper_url = bing_get_wallpaper_url() | |
image_filename = wallpaper_url.split("OHR.")[1].split("&")[0] | |
print("Downloading Wallpaper...") | |
try: | |
img_data = requests.get(BING_URL + wallpaper_url).content | |
with open(image_filename, 'wb') as handler: | |
handler.write(img_data) | |
except RequestException as e: | |
print("Bad image link: ", e) | |
print("Wallpaper " + image_filename + " Downloaded!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment