Scraping HTML Generated by JavaScript – Python

Say we want to get some data from

If we were to do a simple wget or curl on the above url, all we would get would be something like this.

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>CyanogenMod Statistics</title>
    <script src="" type="text/javascript"></script>
    <script src="js/underscore.min.js" type="text/javascript"></script>
    <script src="js/utils.js" type="text/javascript"></script>
    <script src="js/app.js" type="text/javascript"></script>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-responsive.min.css">
    <style type="text/css">
      body {
        padding: 40px;
      table tbody tr td:first-child {
        width: 150px;
    <div class="container">
      <!-- Page Header -->
      <div class="row">
        <div class="span12"><div class="page-header"><h1>CyanogenMod Statistics</h1></div></div>

      <!-- Content -->
      <div id="totalInstalls"></div>
    <script type="text/javascript">
      $(document).ready(function() {

and within the HTML we can see the following lines

      $(document).ready(function() {

this is the bit that generates the actual data we want to download.

The way to do it is to render the page using webkit and download the resulting code.

This solution requires the PyQt4 library with the Qt Framework.

The main part that will do most of the work was taken from here.

All I have done is made it into a separate file so that it can then be imported and incorporated with other things.

So the main file will be called, and will contain the following code.

import sys  
from PyQt4.QtGui import *  
from PyQt4.QtCore import *  
from PyQt4.QtWebKit import *  
class Render(QWebPage):  
    def __init__(self, url):  = QApplication(sys.argv)  
    def _loadFinished(self, result):  
        self.frame = self.mainFrame() 

def getHtml(str_url):
    r_html = Render(str_url)  
    html = r_html.frame.toHtml()

    return html

The main bit that will do the damage is the getHtml() function.

This can then be implemented by importing the above function to another file we will call

from wkRender import getHtml

that is, assuming both files are in the same directory.

Once it has been imported, all we need to do is declare the url and get the HTML as a string.

str_url = ''

str_html = getHtml(str_url)

print str_html

2 replies on “Scraping HTML Generated by JavaScript – Python”

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s