Fake Oakley 'Hidage' 'Amount due'
(datetime.datetime(datetime.datetime.today().year, datetime.datetime.today().month, calendar.monthrange(datetime.datetime.today().year, datetime.datetime.today().month)[1]) - datetime.datetime.today()).total_seconds()
There's a log going on in that one liner. Let's break it down.
The two key Python modules we need to calculate the number of seconds till the end of year are datetime and calendar.
calendar.monthrange(year, month) returns a tuple. The tuple's second element is the number of days in the month.
We create two date objects:
Amazon Simple Storage Service or Amazon S3 is a storage service with a web API. I use Amazon S3 to store backups of my blog and other sites. I made a simple python script to handle file uploads to S3.
In order to use Amazon S3, first create a bucket using your Amazon AWS account. As the name suggests, bucket is a container. You can create buckets using the AWS management console.
The script we're going to write will take two input parameters
A while ago, we discussed how to scrape information from websites that don't offer information in a structured format like XML or JSON. We noted that urllib and lxml are indispensable tools in web scraping. While urllib enables us to connect to websites and retrieve information, lxml helps convert HTML, broken or not, to valid XML and parse it. In this post, I will demonstrate how to retrieve information from web pages that require a login session.
I have created a sample website for this task - http://toscrape.techchorus.net
The website has a page that requires a login session - http://toscrape.techchorus.net/only_authenticated.php
To login to the sample website, use the credentials:
username: admin
password: password
If you visit http://toscrape.techchorus.net/ you will notice that the server sends the response with headers:
Date: Tue, 19 Oct 2010 17:33:43 GMT
Server: Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.14
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=c84456d65e5b9da95b09abd4092f860b; path=/
Location: /login.php
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
In order to maintain the session, you have to send the cookie PHPSESSID with the value c84456d65e5b9da95b09abd4092f860b in all subsequent requests. Of course, the value varies for each user. If the server resets the value of the cookie in a subsequent response, you have to send the updated cookie value in further requests.
Python standard library offers the module cookielib to manage cookies in the client side. We can use this as a cookie jar. In essence, cookielib offers a container to hold cookies. We use this container in urllib2.
Let's start writing the code.
More and more websites are offering APIs nowadays. Previously, we've talked about XML-RPC and REST. Even though web services are growing exponentially there are a lot of websites out there that offer information in unstructured format. Especially, the government websites. If you want to consume information from those websites, web scraping is your only choice.
Web scraping is a technique used in programs that mimic a human browsing the website. In order to scrape a website in your programs you need tools to
In a recently started Pylons project, I wanted to make an easy script appending facility in Mako templates.
The requirement:
<html>
<head>
<title>Some title</title>
<script>...</script>
<script>...</script>
</head>
</%def>
Recent comments
3 weeks 3 days ago
6 weeks 3 days ago
9 weeks 1 day ago
9 weeks 1 day ago
10 weeks 3 days ago
11 weeks 3 hours ago
13 weeks 1 day ago
14 weeks 10 hours ago
32 weeks 2 days ago
33 weeks 3 days ago