Advertisement
LorenKPetrov

Time Grabber

Nov 12th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. # Time grabber by LKP
  2. # Python Version 2.7
  3. # Requires LXML
  4. from lxml import html
  5. import requests
  6. import os
  7.  
  8. print "Which country would you like to know the time of?" # Asks the user which country they would like to know the time of.
  9. country = raw_input("> ") # Country variable
  10. replacement = country.replace(" ","_") # If you enter in a country like "United Kingdom" the website TheTimeNow.com requires an underscore in its URL i.e. http://www.thetimenow.com/United_Kingdom so this replaces a "space" in the two words meaning it doesn't error.
  11. page = requests.get("http://www.thetimenow.com/"+replacement) # Gets the entire page
  12. tree = html.fromstring(page.text) # Converts it into a simple tree for LXML to read
  13. time = tree.xpath('//span[@id="main_time"]/text()') # Reads the Span tag with the ID "main_time"
  14. print "The current time in",country,"is",time[0] # Prints the users selected country along with the current time including PM/AM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement