Reputation: 437
I have this sample code. I read about how superior the Requests is. But I wrote this long time ago with urllib2
.
My question is:
When I run this code in Spyder it works nice. But when I run it from command line (windows 8 64bit) using python mycode.py
it throws error. May anybody please guide me how can I get through this problem? I need to run it from command line. Thanks a lot.
# -*- coding: utf-8 -*-
import urllib2
city = u'Köln'
def make_url(word):
Word = unicode(word)
print type(Word)
url_Word = urllib2.quote(Word, "utf-8")
print "\ntype = %s \n" %type(Word)
print "url_word = %s \n" %url_Word
make_url(city)
result from Spyder:
<type 'unicode'>
type = <type 'unicode'>
url_word = K%F6ln
result from command line:
<type 'unicode'>
C:\Python27\lib\urllib.py:1285: UnicodeWarning: Unicode equal comparison failed
to convert both arguments to Unicode - interpreting them as being unequal
return ''.join(map(quoter, s))
Traceback (most recent call last):
File "to_Ask_question.py", line 21, in <module>
make_url(city)
File "to_Ask_question.py", line 16, in make_url
url_Word = urllib2.quote(Word, "utf-8")
File "C:\Python27\lib\urllib.py", line 1285, in quote
return ''.join(map(quoter, s))
KeyError: u'\xf6'
Upvotes: 0
Views: 1903
Reputation: 437
I got idea from this question.
By just converting to string before feeding to urllib2.quote()
Here is the code:
import urllib2
import codecs
city = u'Köln'
def make_url(word):
word = codecs.encode(word,'utf-8')
print type(word)
url_Word = urllib2.quote(word)
print "\ntype = %s \n" %type(word)
print "url_word = %s \n" %url_Word
make_url(city)
Upvotes: 2