Downloading dynamically loaded webpage with python
<p>I have this <a href="http://bonusbagging.co.uk/oddsmatching.php#" rel="nofollow">website</a> and I want to download the content of the page.</p>

<p>I tried <strong>selenium</strong>, and button clicking with it, but with no success.</p>

<pre><code>#!/usr/bin/env python
from contextlib import closing
from selenium.webdriver import Firefox
import time

# use firefox to get page with javascript generated content
with closing(Firefox()) as browser:
    # setting the url
    browser.get("http://bonusbagging.co.uk/oddsmatching.php#")
    # finding and clicking the button
    button = browser.find_element_by_id('select_button')
    button.click()
    page = browser.page_source
    time.sleep(5)
    print(page.encode("utf8"))
</code></pre>

<p>This code only downloads the source code, where the data are hidden.</p>

<p>Can someone show me the right way to do that? Or tell my how can be the hidden data downloaded?</p>

<p>Thanks in advance!</p>

<p>I always try to avoid selenium like the plague when scraping; it's very slow and is almost never the best way to go about things. You should dig into the source more before scraping; it was clear on this page that the html was coming in and then a separate call was being made to get the table's data. Why not make the same call as the page? It's lightning fast and requires no html parsing; just returns raw data, which seems to be what you're looking for. the python <a href="http://docs.python-requests.org/en/master/" rel="nofollow">requests</a> import is perfect for this. Happy Scraping!</p>

<pre><code>import requests

table_data = requests.get('http://bonusbagging.co.uk/odds-server/getdata_slow.php').content
</code></pre>

<p>PS: The best way to look for these calls is to open the dev console, and check out the network tab. You can see what calls are being made here. Another way is to go to the sources tab, look for some javascript, and search for ajax calls (that's where I got the url I'm calling to above, the path was: top/odds-server.com/odds-server/js/table_slow.js). The later option is sometimes easier, sometimes it's nearly impossible (if the file is minified/uglified). Do whatever works for you!</p>

javascript jquery python html selenium