How to programmatically measure the elements' sizes in HTML source code using python?
Question: I'm doing webpage layout analysis in python. A fundamental task is to
programmatically measure the elements' sizes given HTML source codes, so that
we could obtain statistical data of content/ad ratio, ad block position, ad
block size for the webpage corpus.

An obvious approach is to use the width/height attributes, but they're not
always available. Besides, things like `width: 50%` needs to be calculated
after loading into DOM. So I guess loading the HTML source code into a window-
size-predefined-browser (like
[mechanize](http://wwwsearch.sourceforge.net/mechanize/) although I'm not sure
if window's size could be set) is a good way to try, but mechanize doesn't
support the return of an element size anyway.

Is there any universal way (without width/height attributes) to do it in
python, preferably with some library?

Thanks!


Answer: I suggest You to take a look at [Ghost](http://jeanphix.me/Ghost.py/) \-
webkit web client written in python. It has JavaScript support so you can
easily call JavaScript functions and get its return value. Example shows how
to find out google text box width:

    
    
    >>> from ghost import Ghost
    >>> ghost = Ghost()
    >>> ghost.open('https://google.lt')
    >>> width, resources = ghost.evaluate("document.getElementById('gbqfq').offsetWidth;")
    >>> width
    541.0  # google text box width 541px
    

