Posts

Showing posts from November, 2009

Update twitter status using Python script

Recently I have written a Python script to update status message in twitter using their api . Let me share the code here: import urllib import urllib2 import base64 import httplib import socket ## authenticate module is used for http basic authentication. found in ' urllib2 missing manual ' def authenticate(username, password): password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() top_level_url = 'http://twitter.com/' password_mgr.add_password(None, top_level_url, username, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) opener = urllib2.build_opener(handler) return opener ## updates twitter status given the status, user name (id or email) and password def update_status(status, user, password): data = {'status' : status} data = urllib.urlencode(data) url = 'http://twitter.com/statuses/update.xml' opener = authenticate(user, password) result = '' try: handle = opener.open(url, da

Python UNICODE encode / decode error

Today I was trying to scrape a Spanish site and got into trouble with some Spanish characters. I had to parse some messages from that Spanish website and post into twitter using my Python script. But for some reasons Spanish characters didn't show up in twitter status updates. I was in some trouble with the following error messages: UnicodeDecodeError: 'utf8' codec can't decode bytes in position 5778-5781: invalid data UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 5778: ordinal not in range(128) Then I did Googling for some time and got this very useful link . I used this code to get the content of the website: import codecs import urllib2 url = '' # put the URL here usock = urllib2.urlopen(url) Reader = codecs.getreader("latin_1") fh = Reader(usock) data = fh.read() fh.close() usock.close() data = data.encode("latin_1") Though I first used utf-8 encoding rather than latin_1, but when I got t