Web scraping dynamically loading data in R
<p>I am using R to web scrape data about reviews of 3D printer hubs from here. I need to grab the URL for each of the hubs in the search. I started by using the <code>rvest</code> package, but the data is loading dynamically (I believe using AngularJS) and <code>rvest</code> could not capture it.</p>

<p>After reviewing stackoverflow, I found I could load the webpage onto my computer and save it as a HTML file using phantomjs.org. I did that with the following code.</p>

<pre><code># this example scrapes the user table from:
url &lt;- "https://www.3dhubs.com/3dprint#/?place=New%20York&amp;latitude=40.7144&amp;longitude=-74.006&amp;distanceLimit=250&amp;distanceUnit=miles&amp;shipsToCountry=US&amp;shipsToState=NY"

# write out a script phantomjs can process
writeLines(sprintf("var page = require('webpage').create();
                   page.open('%s', function () {
                   console.log(page.content); //page source
                   phantom.exit();
                   });", url), con="scrape.js")

# process it with phantomjs
system("phantomjs scrape.js &gt; scrape.html")

# use rvest as you would normally use it
page_html &lt;- read_html("scrape.html")
</code></pre>

<p>The above code did not load any of the desired data into R. Then I found the package <code>rdom</code> (<a href="https://github.com/cpsievert/rdom" rel="nofollow">https://github.com/cpsievert/rdom</a>). Rdom uses a similar technique as above, but it was able to load in the names of each of the hubs, but not the link to the hub page. </p>

<pre><code>tbl &lt;- rdom::rdom("https://www.3dhubs.com/3dprint#/?place=New%20York&amp;latitude=40.7144&amp;longitude=-74.006&amp;distanceLimit=250&amp;distanceUnit=miles&amp;shipsToCountry=US&amp;shipsToState=NY")

htmltxt &lt;- paste(capture.output(tbl, file=NULL), collapse="\n")
write(htmltxt, file = "scrape.html")

page_html &lt;- read_html("scrape.html")
</code></pre>

<p>I have a very basic working knowledge of GET and POST requests. So using the Firebug add-in on Firefox, I was able to find the Post request that populates the fields. </p>

<p><a href="https://hub-listings.3dhubs.com/listings" rel="nofollow">https://hub-listings.3dhubs.com/listings</a></p>

<p>In the heading, the website only allows requests from 3dhubs.com. Here is the header for reference: </p>

<pre><code>HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://www.3dhubs.com
access-control-expose-headers: api-version, content-length, content-md5, content-type, date, request-id, response-time
Content-Type: application/json
Date: Wed, 14 Sep 2016 15:48:30 GMT
Content-Length: 227629
Connection: keep-alive
</code></pre>

<p>Is there some other technique I should try? Or does the “Access-Control-Allow-Origin” make it impossible? </p>

<p>Additional question, the search results are paginated. The second page is only loaded in when the “2” is selected at the bottom of the page, but the URL does not change from page 1 to 2. How would you account for this in web scraping? </p>