When I use the following Python 3 URL (https://docs.python.org/3/library/datetime.html) instead of the Python 2 URL (http://docs.python.org/2/library/datetime.html), I get the error message below. It seems unrelated to me to the version of the web page
Here's the error message for the source code below :
`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in
28 for row in table.select('tbody > tr'):
29 tds = row.find_all('td')
---> 30 directive = tds[0].find('span').string
31 # we use getText() here because some of the meanings have extra markup
32 meaning = tds[1].getText().replace('\n', ' ')
AttributeError: 'NoneType' object has no attribute 'string'`
For the following source code :
`#!/usr/bin/env python3
""" Here's the same code but not executed inside a main() function."""
""" Found interesting enumeration of string formatting at https://strftime.org/
From the reference page's github module "build.py" located at
https://github.com/mccutchen/strftime.org"""
#I had to make a few modifications to get it to render the templated ouptut HTML on Windows 10 correctly.
import datetime
import sys
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup
import pystache
url = 'https://docs.python.org/3/library/datetime.html'
body = urllib.request.urlopen(url).read()
soup = BeautifulSoup(body, features="html.parser")
table = soup.find(id='strftime-and-strptime-behavior').find('table')
example_date = datetime.datetime(2013, 9, 30, 7, 6, 5)
directives = []
for row in table.select('tbody > tr'):
tds = row.find_all('td')
directive = tds[0].find('span').string
# we use getText() here because some of the meanings have extra markup
meaning = tds[1].getText().replace('\n', ' ')
example = example_date.strftime(directive)
directives.append({
'directive': directive,
'meaning': meaning,
'example': example
})
# Also add the non zero padded version for some of them
# http://stackoverflow.com/questions/28894172/why-does-d-or-e-remove-the-leading-space-or-zero
exclude = ['%f', '%y']
if 'zero-padded' in meaning and directive not in exclude:
non_zero_padding_decimal = directive.replace("%", "%#")
non_zero_padding_example = example_date.strftime(
non_zero_padding_decimal
)
non_zero_padding_meaning = (
meaning.replace("zero-padded", "") + " (Platform specific)")
directives.append({
'directive': non_zero_padding_decimal,
'meaning': non_zero_padding_meaning,
'example': non_zero_padding_example
})
template_file = '.\strftime.org-master\templates\index.html.mustache'
template = open(template_file).read()
context = {
'example_date': str(example_date),
'example_date_repr': repr(example_date),
'directives': directives,
'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%d'),
}
Render the final context into the HTML template file
rendered_html = pystache.render(template, context)
create / open the output file for writing.
output_html = open("rendered_html.html", "wt")
Now write out the output file.
output_html.write(rendered_html)
output_html.close()
print(pystache.render(template, context))
import webbrowser
webbrowser.open('rendered_html.html')
webbrowser.open('https://docs.python.org/3/library/datetime.html')`