Encode to utf8 before quoting urls

Fixes #315.  Note that python3 works without this commit,
but python2 fails.