<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Boureau on Medium]]></title>
        <description><![CDATA[Stories by David Boureau on Medium]]></description>
        <link>https://medium.com/@bdavidxyz?source=rss-5a6685712cd9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nBaevcQpUuF49irTVWJm5Q.png</url>
            <title>Stories by David Boureau on Medium</title>
            <link>https://medium.com/@bdavidxyz?source=rss-5a6685712cd9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 05:54:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bdavidxyz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Web scraping as a service]]></title>
            <link>https://medium.com/@bdavidxyz/web-scraping-as-a-service-4685a15d1ac3?source=rss-5a6685712cd9------2</link>
            <guid isPermaLink="false">https://medium.com/p/4685a15d1ac3</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[growth-hacking]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[David Boureau]]></dc:creator>
            <pubDate>Thu, 10 Aug 2017 13:19:20 GMT</pubDate>
            <atom:updated>2017-08-10T13:29:40.470Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="http://bdavidxyz.com/blog/web-scraping-as-a-service/">This blog post was originally published on my blog</a></p><p><a href="https://github.com/bdavidxyz/web-scraping-as-a-service/blob/master/index.js">Final code is here</a></p><p><a href="https://bdavidxyz.github.io/ask-the-dude/">Scrapped website is here</a></p><p>Today we’ll study how to create a web service that is able to scrape the web.</p><p>Some requirements could be:</p><ul><li>Ability to extract data from DOM and return it,</li><li>Ability to make some conditional browsing,</li><li>Ability to create a job, with params,</li><li>Ability to retrieve result of this time-consuming job.</li></ul><p>The stack relies on big classics:</p><ul><li>NodeJS</li><li>Git</li><li>NightmareJS</li></ul><p>For the latter : 2 years ago I tried SpookyJS, but I found it quite complicated. Spooky is now not so active, and there is the excellent NightmareJS as a replacement : it allows us to drive a headless browser in a trickless, intuitive way.</p><h3>The website we will scrape</h3><p>We will scrape one of the most technology-advanced AI application ever : ask-the-dude <a href="https://bdavidxyz.github.io/ask-the-dude/">https://bdavidxyz.github.io/ask-the-dude/</a></p><p>We can ask the dude any question :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W4Zhd_ORtpspZgbANqvrvQ.png" /><figcaption>We can ask any question to the dude</figcaption></figure><p>The dude takes time to answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*g1i4_mUHXPYcRcsXiO5Elw.png" /><figcaption>The dude always reply</figcaption></figure><p>It will answer in every cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QPKZYdNvl70FjjNXOyJl8g.png" /></figure><p>Answer may vary, according to the question</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DQ9opBnfgUW3d0uJilykzg.png" /></figure><p>If you forget the trailing “?”, you will have no answer, but in this case, the dude gives you the opportunity to display a random quote.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SsrTgj8R1pMZRcvRbSA3UA.png" /></figure><p>Pretty incredible, isn’t it ?</p><h3>A bad news</h3><p>Unfortunately, the ignoble codeur <strong>didn’t release any API of “the dude.”</strong></p><p>Which means you can’t access to “the dude” programmatically. The only way to get this API is to create a web service that scrapes “the dude”.</p><h3>The API we will create</h3><p><strong>POST /ask</strong> : Ask a question to the dude. The question is in the body of the request. The API replies “OK” to acknowledge the question, but is unable to answer immediately (remember the dude may take time to think about your question and reply properly).</p><p><strong>GET /get-answer-to?q=</strong> : You can use this endpoint a few seconds after the POST : you will get the answer to the question that match with param q.</p><p><strong>GET /all-questions</strong> : display all questions already asked, with their associated answers.</p><h3>Quickstart</h3><pre>$&gt; node --version<br>v6.9.5<br>$&gt; git --version<br>git version 2.7.2<br>$&gt; git clone git@github.com:bdavidxyz/web-scraping-as-a-service.git<br>$&gt; cd web-scraping-as-a-service<br>$&gt; npm install<br>$&gt; npm start</pre><p>Good ! Our service is ready to be tested. You can open it at <a href="http://localhost:5000,">http://localhost:5000/,</a> a welcome message should be printed if everything installed correctly.</p><ul><li>Open Chrome</li><li>Open <a href="http://code.jquery.com/">http://code.jquery.com/</a>, — we will use some simple jQuery code to test our service.</li><li>Open the console from there (cmd+alt+i for Mac users, f12 for Windows users)</li><li>Copy/paste the following code snippets</li></ul><pre>var a = $.ajax({<br> type: &quot;POST&quot;,<br> url: &quot;<a href="http://localhost:5000/ask&#39;">http://localhost:5000/ask</a>&quot;,<br> data: {question:&#39;Do you like butter ?&#39;},<br> success: function(e){console.log(e);}<br>});</pre><p>“OK” should be printed. Wait a few seconds, then</p><pre>var a = $.ajax({<br> type: &quot;GET&quot;,<br> url: &quot;<a href="http://localhost:5000/ask&#39;">http://localhost:5000/</a>get-answer-for?q=Do you like butter ?&quot;,<br> success: function(e){console.log(e);}<br>});</pre><p>You should have the answer to the question.</p><p>Try now to get an answer to a question you never asked :</p><pre>var a = $.ajax({<br> type: &quot;GET&quot;,<br> url: &quot;http://localhost:5000/get-answer-for?q=WTF ?&quot;,<br> success: function(e){console.log(e);}<br>});</pre><p>You can print all questions</p><pre>var a = $.ajax({<br> type: &quot;GET&quot;,<br> url: &quot;http://localhost:5000/all-questions&quot;,<br> success: function(e){console.log(e);}<br>});</pre><h3>Code</h3><h4>Ask a question</h4><p>The relevant part is here : <a href="https://github.com/bdavidxyz/web-scraping-as-a-service/blob/master/index.js#L24-L69">https://github.com/bdavidxyz/web-scraping-as-a-service/blob/master/index.js#L24-L69</a></p><p>See that NightmareJS is <strong>pretty intuitive</strong> : you can chain basic instructions very easily. However the famous JS pyramid nightmare (ahem), cannot be completely avoided : once you start to evaluate anything on the page, the result of this evaluation is wrapped in a promise.</p><p>You have to be very careful about these 3 things :</p><ul><li><a href="https://github.com/bdavidxyz/web-scraping-as-a-service/blob/master/index.js#L60">don’t forget</a> to end your nightmare instance once you used it.</li><li><a href="https://github.com/bdavidxyz/web-scraping-as-a-service/blob/master/index.js#L47">don’t forget</a> the keyword “return” in front of the nightmare instance inside the promise, or chaining of promises will not occur.</li><li><a href="https://github.com/bdavidxyz/web-scraping-as-a-service/blob/master/index.js#L35">don’t forget</a> to re-instantiate NightmareJS on every request.</li></ul><p>Notice that in this example, you can achieve <strong>conditional browsing</strong> : based on the result of a first evaluation, you can reuse the nightmare instance and scrape the web page again.</p><h4>Other parts</h4><p>Great ! Who can do more can do less.</p><p>The two other endpoints <a href="https://github.com/bdavidxyz/web-scraping-as-a-service/blob/master/index.js#L84-L99">/get-answer-to?q=</a> and <a href="https://github.com/bdavidxyz/web-scraping-as-a-service/blob/master/index.js#L79-L82">/all-questions </a>don’t use NightmareJS, they are simple, self-describing ExpressJS endpoints.</p><h3>Concluding thoughts</h3><p>We have now :</p><ul><li>A completely free way to scrape any website, for free,</li><li>A way to APIfy any website that is not accessible by any other mean than a web browser,</li><li>A way to create some background job,</li><li>A way to do some conditional browsing.</li></ul><p>Possible improvements :</p><ul><li>You can deploy the service to a service like Heroku to make your API accessible from anywhere.</li><li>I admit that this doesn’t scale very well, for intensive tasks you may want to use more serious background jobs solutions, and polling an API is also not a very good idea. But that’s enough for today :)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4685a15d1ac3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>