0

I am using bottle to host a simple html page which changes the page title on load. The HTML page code:-

<html>
    <head>
        <title>title</title>
        <script type="text/javascript">
            function initialize(){
                var z=1234;
                document.title = z;}
        </script>
    </head>
    <body onload="initialize();">
        hi
    </body>
</html>

My bottle hosting code:

from bottle import route, run, template

@route('/:anything')
def something(anything=''):
   return template('C:/test1.html')

run(host='localhost', port=8080)

I am trying to capture the updated document.title using python. so far I have tried urllib,mechanize,htmlparse but all of them were returning "title" instead of 1234.

a sample mechanize code that I have tried is:

from mechanize import Browser
br = Browser()
br.open("http://localhost:8080/hello")
print br.title()

Please help me.

S4nd33p
  • 431
  • 2
  • 7
  • 17
  • The title is updated using JavaScript, which most Python HTTP-client implementations will not evaluate. If the JavaScript on the website is really that simple, you could try extracting the `z=1234` part instead. – n.st Dec 07 '13 at 15:06
  • :) thanks for your response, but you got me there. I have diluted the logic in order to make my question simple but the value of z is not that easily available. actually the value z is assigned like this.. var z = google.maps.geometry.spherical.computeArea(shape.getPath().getArray()); So do you see any way for me to extract that value out? – S4nd33p Dec 07 '13 at 15:11
  • Looks like there are in fact ways to execute JavaScript on websites using Python: http://blog.motane.lu/2009/06/18/pywebkitgtk-execute-javascript-from-python/ http://stackoverflow.com/a/8049747/1114687 – n.st Dec 07 '13 at 15:14
  • looks promising ... checking them out..thanks again. :) – S4nd33p Dec 07 '13 at 15:17
  • Ok, good luck! I've flagged the question as a duplicate of [Web-scraping JavaScript page with Python](http://stackoverflow.com/questions/8049520/web-scraping-javascript-page-with-python), so anyone finding the question via the search function will find the answer linked above more easily. – n.st Dec 07 '13 at 15:18

0 Answers0