Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[Python] - Time Grabber

python time grabber scrape lxml timezone

  • Please log in to reply
1 reply to this topic

#1 Sundance

Sundance

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 572 posts
  • Programming Language:C, Java, PHP, Python, JavaScript, Perl, PL/SQL, Transact-SQL, Bash, Others

Posted 12 November 2013 - 05:15 PM

So during a little bit of free time I wrote this

I have commented on the code, it's a very simple time scraper!

# Time grabber by LKP
# Python Version 2.7
# Requires LXML
from lxml import html
import requests
import os

print "Which country would you like to know the time of?" # Asks the user which country they would like to know the time of.
country = raw_input("> ") # Country variable
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.
page = requests.get("http://www.thetimenow.com/"+replacement) # Gets the entire page
tree = html.fromstring(page.text) # Converts it into a simple tree for LXML to read
time = tree.xpath('//span[@id="main_time"]/text()') # Reads the Span tag with the ID "main_time"
print "The current time in",country,"is",time[0] # Prints the users selected country along with the current time including PM/AM

Hope you enjoy it!


  • 1

Please read the

FaQ & Guidelines


#2 Anubis

Anubis

    CC Newcomer

  • Member
  • PipPip
  • 19 posts
  • Location:Croatia
  • Programming Language:Python
  • Learning:C++, Visual Basic .NET

Posted 15 November 2013 - 06:26 AM

That's nice! Very simple, yet very complicated for me. I understand the code, but I don't know if I could write it myself. I'm still waiting for my Python books to arrive. :) Thanks for the share!


  • 1





Also tagged with one or more of these keywords: python, time, grabber, scrape, lxml, timezone

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download