@kovid: the problem is the table embedded in a table in narrow view. If I skip the outermost <td> when simplifying the HTML then it sort-of works. By 'sort-of' I mean that the output has line breaks etc but isn't the same as one gets in wide view. Another problem: passing to do_copy() whether it is narrow or wide -- this seems to require a signature change. Better would be to encode this into the HTML, perhaps as an identifier in the extra <table>.
I don't follow the HTML manipulations you are doing and am not skilled with etree etc, so I think me trying to fix it correctly will just waste your time.