Kenya's parliament has elected an opposition lawmaker as speaker, in a close vote reflecting deep and bitter political divisions.