3

I try to login to partnernet.amazon.de (German amazon affiliate program).

This is my code so far:

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"

The script fails with: 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 sign_in form.

`[(u'__mk_de_DE', u'\xc5M\xc5\u017d\xd5\xd1'), ...] # ÅMÅŽÕÑ`

So i know that it is an encoding issue and where it happens. What i can't figure out is how/where to set the right encoding to prevent this error.

Update: The error is reproducible with the code above. There is no need to have real login details.

RandomDude
  • 1,101
  • 18
  • 33
  • Related: [urllib.urlencode doesn't like unicode values: how about this workaround?](http://stackoverflow.com/q/6480723/2173773). It seems like `mechanize` and `urllib` does not handle unicode in form controls for Python 2 – Håkon Hægland Apr 10 '17 at 20:33

0 Answers0