Login with Python-Mechanize results in UnicodeEncodeError
I am trying to login to partnernet.amazon.de ( german amazon affiliate program).
This is my code:
import cookielib
import mechanize
# Browser
br = mechanize.Browser()
# Enable cookie support for urllib2
cookiejar = cookielib.LWPCookieJar()
br.set_cookiejar( cookiejar )
# Broser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
#
br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1 )
br.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36' ) ]
# authenticate
br.open('https://partnernet.amazon.de/gp/associates/join/landing/')
br.select_form(name="sign_in")
br['username'] = 'username'
br['password'] = 'password'
res = br.submit() # error occurs here
print "Success!\n"
Failure script:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc5' in position 0: ordinal not in range(128)
.
The error occurs because of this parameter of the form sign_in.
`[(u'__mk_de_DE', u'\xc5M\xc5\u017d\xd5\xd1'), ...] # Ã
MÃ
ÅŊÃÃ`
So I know this is a coding problem and where it is going. I can't figure out how / where to set the correct encoding to prevent this error.
Update: The bug is reproducible with the above code. You don't need to have real login details.
+3
source to share
No one has answered this question yet
See similar questions:
or similar: