Posted By

Keef on 11/07/10


Tagged

windows beautifulsoup mechanize


Versions (?)

Grab a random wallpaper from wallbase.net


 / Published in: Python
 

This has been made for Windows but shouldn't be hard to convert for other OS's.

  1. import re
  2. import mechanize
  3. from BeautifulSoup import BeautifulSoup
  4. from array import array
  5. import random
  6. import ctypes
  7.  
  8.  
  9. # setup
  10. br = mechanize.Browser()
  11. links = []
  12. random.seed()
  13. br.set_handle_robots(False)
  14. br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
  15.  
  16. # grab the index page
  17. br.open("http://wallbase.net/random")
  18.  
  19. # get a list of links
  20. for link in br.links(url_regex="wallbase.net/wallpaper/"):
  21. links.append(link.url)
  22.  
  23. # grab a random link, also convert list item to string
  24. rndurl = str(random.sample(links, 1)).strip('[]\'')
  25. response = br.open(rndurl)
  26. soup = BeautifulSoup(response.read())
  27. imgurl = soup.find("div", { "id" : "bigwall" }).findNext('img')['src']
  28.  
  29. response2 = br.open(imgurl)
  30.  
  31. # save wallpaper
  32. file = open("wallpaper.jpg","wb")
  33. file.write(response2.get_data())
  34. file.close()
  35.  
  36. # set wallpaper
  37. SPI_SETDESKWALLPAPER = 20
  38. MY_WALLPAPER = "wallpaper.jpg"
  39. ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, MY_WALLPAPER , 0)

Report this snippet  

You need to login to post a comment.