Scraping AJAX web pages (Part 1)
Don’t forget to check out the rest of the series too!
Problem
You want to download a web page whose source if full of AJAX calls. You want the result that is shown in your browser, i.e. you want the generated (post-AJAX) source.
Example
Consider this simple page: test.html. If you open it in your browser, you’ll see the text “Hi Crowbar!”. However, if you download this page with wget for instance, in the source code you’ll see the text “Hi lame crawler”. Explanation: your browser downloads and then interprets the page. The executed JavaScript code updates the DOM of the page. A simple downloader like wget doesn’t interpret the source of a page just grabs it.
Solution #1
One way for getting the post-AJAX source of a web page is to use Crowbar. “Crowbar is a web scraping environment based on the use of a server-side headless mozilla-based browser. Its purpose is to allow running javascript scrapers against a DOM to automate web sites scraping…”
When you launch Crowbar, it offers a RESTful web service listening by default on port 10000. Just open the page http://127.0.0.1:10000/. The trick behind Crowbar is that it turns a web browser into a web server.
Now we can download AJAX pages with wget the following way. Let’s get the previous test page:
wget "http://127.0.0.1:10000/?url=http://simile.mit.edu/crowbar/test.html" -O tricky.html
If you check the source of the saved file, you will see the post-AJAX source that you would normally see in a web browser. You can also pass some other parameters to the Crowbar web service, they are detailed here. The most important parameter is “delay” that tells Crowbar how much it should wait after the page has terminated loading before attempting to serialize its DOM. By default its value is 3000 msec, i.e. 3 sec. If the page you want to download contains lots of AJAX calls then consider increasing the delay, otherwise you will get an HTML source that is not fully expanded yet.
Use case:
I wanted to download the following page from the NCBI database: CP002059.1. The page is quite big (about 5 MB), thus I had to wait about 10 sec. to get it in my browser. From the command-line I could fetch it this way (I gave it some extra time to be sure):
wget "http://127.0.0.1:10000/?url=http://www.ncbi.nlm.nih.gov/nuccore/CP002059.1&delay=15000" -O CP002059.1.html
Notes: If you want to download data from NCBI, there is a better way.
Did you know?
In Firefox, if you look at the source of a page (View -> Page Source), you will see the downloaded (pre-AJAX) source. If you want to see the generated (post-AJAX) source, you can use the Web Developer add-on (View Source -> View Generated Source).
Also, still in Firefox, if you save a web page with File -> Save Page As… and you choose “Web Page, HTML only”, Firefox will save the original (pre-AJAX) source. If you want the fully expanded (generated) source, choose the option “Web Page, complete”.
Solution #2
Another solution is to write a program/script that uses the webkit open source browser engine. In an upcoming post I will show you how to do it with Python.
Appendix
Crowbar launch script for Linux:
#!/usr/bin/bash # my crowbar is installed here: /opt/crowbar # location of this file: /opt/crowbar/start.sh xulrunner --install-app xulapp xulrunner xulapp/application.ini
Crowbar launch script for Windows (update of 20110601):
rem My crowbar is installed here: c:\Program Files\crowbar rem Location of this file: c:\Program Files\crowbar\start.bat "%XULRUNNER_HOME%\xulrunner.exe" --install-app xulapp "%XULRUNNER_HOME%\xulrunner.exe" xulapp\application.ini
XULRunner for Windows can be downloaded from here.
/ discussion /
Leave a comment Cancel reply
Blog Stats
- 1,092,851 hits
Random Post
Recent Posts
Categories
Archives
- March 2024 (1)
- February 2024 (3)
- November 2023 (1)
- August 2023 (2)
- June 2023 (4)
- March 2023 (2)
- February 2023 (1)
- January 2023 (1)
- December 2022 (3)
- November 2022 (1)
- September 2022 (2)
- July 2022 (2)
- April 2022 (1)
- March 2022 (1)
- February 2022 (2)
- January 2022 (3)
- October 2021 (5)
- September 2021 (5)
- May 2021 (1)
- April 2021 (2)
- January 2021 (1)
- December 2020 (1)
- November 2020 (2)
- October 2020 (8)
- September 2020 (1)
- August 2020 (1)
- July 2020 (1)
- April 2020 (2)
- March 2020 (2)
- February 2020 (8)
- January 2020 (2)
- December 2019 (5)
- November 2019 (3)
- September 2019 (1)
- August 2019 (2)
- June 2019 (3)
- May 2019 (1)
- March 2019 (1)
- February 2019 (4)
- January 2019 (1)
- December 2018 (3)
- November 2018 (5)
- October 2018 (9)
- September 2018 (1)
- August 2018 (1)
- June 2018 (1)
- May 2018 (3)
- April 2018 (2)
- March 2018 (5)
- February 2018 (1)
- January 2018 (7)
- December 2017 (9)
- November 2017 (4)
- October 2017 (7)
- September 2017 (10)
- August 2017 (1)
- July 2017 (2)
- June 2017 (2)
- May 2017 (5)
- April 2017 (3)
- March 2017 (3)
- February 2017 (4)
- January 2017 (10)
- December 2016 (1)
- November 2016 (2)
- October 2016 (7)
- September 2016 (5)
- August 2016 (10)
- July 2016 (4)
- June 2016 (11)
- May 2016 (11)
- April 2016 (5)
- March 2016 (11)
- February 2016 (9)
- January 2016 (2)
- December 2015 (7)
- November 2015 (7)
- October 2015 (9)
- September 2015 (8)
- August 2015 (11)
- July 2015 (5)
- June 2015 (5)
- May 2015 (11)
- April 2015 (7)
- March 2015 (9)
- February 2015 (5)
- January 2015 (7)
- December 2014 (13)
- November 2014 (18)
- October 2014 (2)
- September 2014 (5)
- July 2014 (10)
- June 2014 (7)
- May 2014 (10)
- April 2014 (3)
- March 2014 (3)
- February 2014 (13)
- January 2014 (8)
- December 2013 (9)
- November 2013 (10)
- October 2013 (10)
- September 2013 (15)
- August 2013 (6)
- July 2013 (7)
- June 2013 (12)
- May 2013 (12)
- April 2013 (20)
- March 2013 (10)
- February 2013 (7)
- January 2013 (26)
- December 2012 (18)
- November 2012 (13)
- October 2012 (9)
- September 2012 (8)
- August 2012 (7)
- July 2012 (3)
- June 2012 (9)
- May 2012 (16)
- April 2012 (32)
- March 2012 (18)
- February 2012 (17)
- January 2012 (12)
- December 2011 (8)
- November 2011 (18)
- October 2011 (18)
- September 2011 (32)
- August 2011 (13)
- July 2011 (16)
- June 2011 (6)
- May 2011 (14)
- April 2011 (30)
- March 2011 (45)
- February 2011 (42)
- January 2011 (28)
- December 2010 (14)
- November 2010 (29)
- October 2010 (15)
- September 2010 (5)
Hi,
I tried crowbar with the test page you provided. It works fine. However, I tried it with translate.google.com page for two languages and a simple word and it did not return rendered html file.
Could you please let me know how it could be fixed.
Thanks.
Alex.
My attempt with Google Translate failed too. It seems it’s protected somehow. It’s better to use an API for Translate, which is provided by Google. Here you can find a Python solution with some examples: http://www.catonmat.net/blog/python-library-for-google-translate/.
Hi, thanks for the info on Crowbar. It is the most complete I’ve seen.
I’m having a problem: When I navigate to page http://127.0.0.1:10000/
The browser displays:
“127.0.0.1” is not set up to establish an connection on port 10000 with this computer.
When I use wget: wget “http://127.0.0.1:10000/?url=http://www.ncbi.nlm.nih.gov/nuccore/CP002059.1&delay=15000” -O CP002059
The command line prompt displays:
“Connection refused”
Any help would be greatly appreciated.
By the way, I am developing a scrapping application in VB.NET and I am using htmlagilitypack for parsing HTML tags.
The big problem I’ve run into is that I am unable to figure out the font size of text when the font size is specified by CSS class. If the CSS style of an element is from an External Style Sheet it is potentially an even more difficult problem to solve.
I was hoping that Crowbar could be used to get the computed CSS style of various html elements in pages that I scrape.
Perhaps there is something very basic that I don’t understand about what Crowbar is useful for, and any guidance you can give would be greatly appreciated.
Thanks,
Mark
Do you use Windows? I think that because of VB.NET… I tried this method under Linux, I don’t know if Crowbar works with Windows.
Edit: sorry, I checked the Crowbar home page and they say it runs under Windows. I will try it and tell you how far I got.
Edit #2: I could install Crowbar under Windows without any problem. (For checking out the Subversion repository, I used the svn command from the Cygwin environment). In the text of the post above I also added a launch script. When I launched it, Windows asked me if I want to allow or block it. “Allow” it. So your problem must be a firewall problem, Windows blocks the given port. Look around the settings of your Firewall how to allow the port 10000.
After that how you extract the desired data from CSS, now it’s up to you :)
can a single instance of Crowbar fetch multiple web pages simultaneously ?
Currently, for a java application, I am resorting to multithreading and using mutual exclusion by threads to fetch a page.
Can this be done concurrently ?