Scraping Javascript page using Python

October 18, 2014

Python library dryscape can be used to scrape javascript driven websites.


To give an example, I created a sample page with following HTML code. (link):

<!DOCTYPE html>
  <meta charset="utf-8">
  <title>Javascript scraping test</title>
  <p id='intro-text'>No javascript support</p>
     document.getElementById('intro-text').innerHTML = 'Yay! Supports javascript';

without javascript it says: No javascript support and with javascript: Yay! Supports javascript

Scraping without JS support:

>>> import requests
>>> from bs4 import BeautifulSoup
>>> response = requests.get(my_url)
>>> soup = BeautifulSoup(response.text)
>>> soup.find(id="intro-text")
<p id="intro-text">No javascript support</p>

Scraping with JS support:

>>> import dryscrape
>>> from bs4 import BeautifulSoup
>>> session = dryscrape.Session()
>>> session.visit(my_url)
>>> response = session.body()
>>> soup = BeautifulSoup(response)
>>> soup.find(id="intro-text")
<p id="intro-text">Yay! Supports javascript</p>