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
python mechanize


source to share


No one has answered this question yet

See similar questions:

47
urllib.urlencode doesn't like unicode values: how about this workaround?

or similar:

5504
Does Python have a ternary conditional operator?
5231
What are metaclasses in Python?
4473
Calling an external command in Python
3790
How can I safely create a subdirectory?
3602
Does Python have a substring method "contains"?
3119
What is the difference between Python list methods that are appended and expanded?
2818
Finding the index of an element by specifying the list that contains it in Python
2601
How can I make a time delay in Python?
2568
How to find the current time in Python
2097
Is there a way to run Python on Android?



All Articles
Loading...
X
Show
Funny
Dev
Pics