\\n'\n",
+ " '
\\n'\n",
+ " ' \\n'\n",
+ " ' How do I get the current time in Python?\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' The\\n'\n",
+ " ' \\n'\n",
+ " ' time\\n'\n",
+ " '
\\n'\n",
+ " ' module\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' The\\n'\n",
+ " ' \\n'\n",
+ " ' time\\n'\n",
+ " '
\\n'\n",
+ " ' module provides functions that tells us the time in \"seconds '\n",
+ " 'since the epoch\" as well as other utilities.\\n'\n",
+ " '
\\n'\n",
+ " '
import time\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' Unix Epoch Time\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' This is the format you should get timestamps in for saving in '\n",
+ " 'databases. It is a simple floating point number that can be converted to an '\n",
+ " 'integer. It is also good for arithmetic in seconds, as it represents the '\n",
+ " 'number of seconds since Jan 1, 1970 00:00:00, and it is memory light '\n",
+ " \"relative to the other representations of time we'll be looking at next:\\n\"\n",
+ " '
\\n'\n",
+ " '
>>> time.time()\\n'\n",
+ " '1424233311.771502\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " \" This timestamp does not account for leap-seconds, so it's not \"\n",
+ " 'linear - leap seconds are ignored. So while it is not equivalent to the '\n",
+ " 'international UTC standard, it is close, and therefore quite good for most '\n",
+ " 'cases of record-keeping.\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' This is not ideal for human scheduling, however. If you have a '\n",
+ " \"future event you wish to take place at a certain point in time, you'll want \"\n",
+ " 'to store that time with a string that can be parsed into a datetime object '\n",
+ " 'or a serialized datetime object (these will be described later).\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' \\n'\n",
+ " ' time.ctime\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' You can also represent the current time in the way preferred by '\n",
+ " 'your operating system (which means it can change when you change your system '\n",
+ " \"preferences, so don't rely on this to be standard across all systems, as \"\n",
+ " \"I've seen others expect). This is typically user friendly, but doesn't \"\n",
+ " 'typically result in strings one can sort chronologically:\\n'\n",
+ " '
\\n'\n",
+ " '
>>> time.ctime()\\n'\n",
+ " \"'Tue Feb 17 23:21:56 2015'\\n\"\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' You can hydrate timestamps into human readable form with\\n'\n",
+ " ' \\n'\n",
+ " ' ctime\\n'\n",
+ " '
\\n'\n",
+ " ' as well:\\n'\n",
+ " '
\\n'\n",
+ " '
>>> time.ctime(1424233311.771502)\\n'\n",
+ " \"'Tue Feb 17 23:21:51 2015'\\n\"\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' This conversion is also not good for record-keeping (except in '\n",
+ " 'text that will only be parsed by humans - and with improved Optical '\n",
+ " 'Character Recognition and Artificial Intelligence, I think the number of '\n",
+ " 'these cases will diminish).\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' \\n'\n",
+ " ' datetime\\n'\n",
+ " '
\\n'\n",
+ " ' module\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' The\\n'\n",
+ " ' \\n'\n",
+ " ' datetime\\n'\n",
+ " '
\\n'\n",
+ " ' module is also quite useful here:\\n'\n",
+ " '
\\n'\n",
+ " '
>>> import datetime\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' \\n'\n",
+ " ' datetime.datetime.now\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' The\\n'\n",
+ " ' \\n'\n",
+ " ' datetime.now\\n'\n",
+ " '
\\n'\n",
+ " ' is a class method that returns the current time. It uses the\\n'\n",
+ " ' \\n'\n",
+ " ' time.localtime\\n'\n",
+ " '
\\n'\n",
+ " ' without the timezone info (if not given, otherwise see timezone '\n",
+ " 'aware below). It has a representation (which would allow you to recreate an '\n",
+ " 'equivalent object) echoed on the shell, but when printed (or coerced to a\\n'\n",
+ " ' \\n'\n",
+ " ' str\\n'\n",
+ " '
\\n'\n",
+ " ' ), it is in human readable (and nearly ISO) format, and the '\n",
+ " 'lexicographic sort is equivalent to the chronological sort:\\n'\n",
+ " '
\\n'\n",
+ " '
>>> datetime.datetime.now()\\n'\n",
+ " 'datetime.datetime(2015, 2, 17, 23, 43, 49, 94252)\\n'\n",
+ " '>>> print(datetime.datetime.now())\\n'\n",
+ " '2015-02-17 23:43:51.782461\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " \" datetime's\\n\"\n",
+ " ' \\n'\n",
+ " ' utcnow\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' You can get a datetime object in UTC time, a global standard, '\n",
+ " 'by doing this:\\n'\n",
+ " '
\\n'\n",
+ " '
>>> datetime.datetime.utcnow()\\n'\n",
+ " 'datetime.datetime(2015, 2, 18, 4, 53, 28, 394163)\\n'\n",
+ " '>>> print(datetime.datetime.utcnow())\\n'\n",
+ " '2015-02-18 04:53:31.783988\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' UTC is a time standard that is nearly equivalent to the GMT '\n",
+ " 'timezone. (While GMT and UTC do not change for Daylight Savings Time, their '\n",
+ " 'users may switch to other timezones, like British Summer Time, during the '\n",
+ " 'Summer.)\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' datetime timezone aware\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " \" However, none of the datetime objects we've created so far can \"\n",
+ " 'be easily converted to various timezones. We can solve that problem with '\n",
+ " 'the\\n'\n",
+ " ' \\n'\n",
+ " ' pytz\\n'\n",
+ " '
\\n'\n",
+ " ' module:\\n'\n",
+ " '
\\n'\n",
+ " '
>>> import pytz\\n'\n",
+ " '>>> then = datetime.datetime.now(pytz.utc)\\n'\n",
+ " '>>> then\\n'\n",
+ " 'datetime.datetime(2015, 2, 18, 4, 55, 58, 753949, tzinfo=<UTC>)\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' Equivalently, in Python 3 we have the\\n'\n",
+ " ' \\n'\n",
+ " ' timezone\\n'\n",
+ " '
\\n'\n",
+ " ' class with a utc\\n'\n",
+ " ' \\n'\n",
+ " ' timezone\\n'\n",
+ " '
\\n'\n",
+ " ' instance attached, which also makes the object timezone aware '\n",
+ " '(but to convert to another timezone without the handy\\n'\n",
+ " ' \\n'\n",
+ " ' pytz\\n'\n",
+ " '
\\n'\n",
+ " ' module is left as an exercise to the reader):\\n'\n",
+ " '
\\n'\n",
+ " '
>>> '\n",
+ " 'datetime.datetime.now(datetime.timezone.utc)\\n'\n",
+ " 'datetime.datetime(2015, 2, 18, 22, 31, 56, 564191, '\n",
+ " 'tzinfo=datetime.timezone.utc)\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' And we see we can easily convert to timezones from the original '\n",
+ " 'utc object.\\n'\n",
+ " '
\\n'\n",
+ " '
>>> print(then)\\n'\n",
+ " '2015-02-18 04:55:58.753949+00:00\\n'\n",
+ " \">>> print(then.astimezone(pytz.timezone('US/Eastern')))\\n\"\n",
+ " '2015-02-17 23:55:58.753949-05:00\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' You can also make a naive datetime object aware with the\\n'\n",
+ " ' \\n'\n",
+ " ' pytz\\n'\n",
+ " '
\\n'\n",
+ " ' timezone\\n'\n",
+ " ' \\n'\n",
+ " ' localize\\n'\n",
+ " '
\\n'\n",
+ " ' method, or by replacing the tzinfo attribute (with\\n'\n",
+ " ' \\n'\n",
+ " ' replace\\n'\n",
+ " '
\\n'\n",
+ " ' , this is done blindly), but these are more last resorts than '\n",
+ " 'best practices:\\n'\n",
+ " '
\\n'\n",
+ " '
>>> '\n",
+ " 'pytz.utc.localize(datetime.datetime.utcnow())\\n'\n",
+ " 'datetime.datetime(2015, 2, 18, 6, 6, 29, 32285, tzinfo=<UTC>)\\n'\n",
+ " '>>> datetime.datetime.utcnow().replace(tzinfo=pytz.utc)\\n'\n",
+ " 'datetime.datetime(2015, 2, 18, 6, 9, 30, 728550, tzinfo=<UTC>)\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' The\\n'\n",
+ " ' \\n'\n",
+ " ' pytz\\n'\n",
+ " '
\\n'\n",
+ " ' module allows us to make our\\n'\n",
+ " ' \\n'\n",
+ " ' datetime\\n'\n",
+ " '
\\n'\n",
+ " ' objects timezone aware and convert the times to the hundreds of '\n",
+ " 'timezones available in the\\n'\n",
+ " ' \\n'\n",
+ " ' pytz\\n'\n",
+ " '
\\n'\n",
+ " ' module.\\n'\n",
+ " '
\\n'\n",
+ " '
\\n'\n",
+ " ' One could ostensibly serialize this object for UTC time and '\n",
+ " 'store\\n'\n",
+ " ' \\n'\n",
+ " ' that\\n'\n",
+ " ' \\n'\n",
+ " ' in a database, but it would require far more memory and be more '\n"
+ ]
},
{
- "cell_type": "code",
- "metadata": {
- "id": "7TbFVmpRg6jN",
- "colab_type": "code",
- "outputId": "fea34a67-acf5-4fa0-c88a-e604146689e4",
- "colab": {
- "base_uri": "https://localhost:8080/",
- "height": 1000
- }
- },
- "source": [
- "pprint(soupified.prettify()) #to get an idea of the html structure of the webpage"
- ],
- "execution_count": 0,
- "outputs": [
- {
- "output_type": "stream",
- "text": [
- "\u001b[1;30;43mStreaming output truncated to the last 5000 lines.\u001b[0m\n",
- " ' \\n'\n",
- " ' \\n'\n",
- " '
![\"\"]()
\\n'\n",
- " '
\\n'\n",
- " ' \\n'\n",
- " '
\\n'\n", + " '- \\n'\n",
+ " '
\\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' 8\\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' please note, the most voted answers are for timezonoe-naive '\n",
+ " 'datetime, while we see that in production environment more and more services '\n",
+ " 'across the world are connected together and timezone-aware datetime become '\n",
+ " 'the required standard\\n'\n",
+ " ' \\n'\n",
+ " ' –\\n'\n",
+ " ' \\n'\n",
+ " ' Sławomir Lenart\\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' Apr 29 at 17:12\\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " ' \\n'\n",
+ " '
\\n'\n", + " '