How To Create Automatically Wallpaper Updater (Bing Wallpaper) Using Python - Web Scraping - part 8

Hello Friends,


                           This is our eighth part of web scraping tutorials. and in this tutorial, I am gonna to show you how to create automatically wallpaper changer program using python . but if you are new visitor then first check our index or For seventh Part Click Here


So, 
Let's Talk About Today's Topic.

Friends, Here We will try to create python script that can download wallpaper from bingwallpaper.com.
and
if you want  to use this example  for daily purpose then i will suggest you to visit this project.

https://github.com/UtkarshGpta/bing-desktop-wallpaper-changer because this project is really good for daily purpose automation.

In this script,

first we will download bing wallpaper xml archive. why?

because bingwallpaper.com provides an xml services and we will use this for images url link and other related data of image.

and another big reason is, xml are easy to scrap.

after getting images downloading url link.

we will download images from that link and save on local directory.

now, our other next step is to set that image, our desktop wallpaper. and here, for this purpose, we will use linux built-in tool.

 I know, this example also looks very difficult. but don't worry, friends i know you can do it.


Here, I am Sharing My Demo Codes But If You Want More Better Example Then, You Can Modify these codes yourself or Download This Script From My GitHub repository (link given at the end of these codes ).

1. wallpaper_updater/main.py




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Written By:
#  S.S.B
#  surajsinghbisht054@gmail.com
#  bitforestinfo.blogspot.com
#
# 
# Import Module
import re
import urllib2
import os

# Latest Homepage xml Url
xml_url = 'http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=2&mkt=en-us'

# Fucntion For Extracting Image Url and Image Details From xml
def get_image_details(xml):
 # Url and Details Pattern
 image_url_pattern = '<url>(.*?)</url>.+<copyright>(.*?)</copyright>'
 
 # search patterns 
 (img_url, img_detail) = re.search(image_url_pattern, xml).groups(1)

 # Complete Url
 bing = 'http://www.bing.com'+img_url
 return (bing, img_detail)

# Function for Downloading Image
def download_image(url):
 # Choose Image Name
 filename = url.split('/')[-1]
 img_file = open(filename, 'wb')
 # Download Image
 img_file.write(urllib2.urlopen(url).read())
 # Save Image
 img_file.close()

 # return Image Name
 return filename

# Function For Changing Wallpaper Automatically
def change_wallpaper(filename):
 # Get Absolute Path Of Image
 path = os.path.join(os.path.realpath('.'),filename)
 # Command For Changing Wallpaper
 cmd = 'gsettings set org.gnome.desktop.background picture-uri "file:///{}"'.format(path)
 os.system(cmd)
 return
 
# Main Function
def main():
 # Download Xml Data
 raw_xml = urllib2.urlopen(xml_url).read()

 # Extract Image Url From Xml
 (img_url, img_detail) = get_image_details(raw_xml)

 # Download Image
 filename = download_image(img_url)

 # Change Desktop Wallpaper
 change_wallpaper(filename)

 return

# Main Function Trigger
if __name__ == '__main__':
 main()

Warning I am Creating This Tutorial Only For Practise and Educational Purpose. I will not Take any type of responsibility about any illegal activities.

For Downloading, Raw Script Click Here


In Our Future Tutorials, we will try to create youtube video downloader, live score scraper, email harvester , Amazon product details scraper and many other scripts also. 

For More Update, Visit Our Regularly. 
And Subscribe Our Blog, 


Follow Us and share it.
For Any Type of Suggestion Or Help
Contact me:
S.S.B
surajsinghbisht054@gmail.com

Share this

Related Posts

Previous
Next Post »