<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/scraping-a-website-with-5-lines-of-r-code/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2018/01/scraping-with-5-lines-r.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>In what is rapidly becoming a series — <a rel="nofollow" href="http://blog.revolutionanalytics.com/2017/11/charts-in-280-chars.html" target="_blank">cool things you can do with R in a tweet</a> — Julia Silge demonstrates scraping the list of <a rel="nofollow" href="https://en.wikipedia.org/wiki/Current_members_of_the_United_States_House_of_Representatives" target="_blank">members of the US house of representatives on Wikipedia</a> in just 5 R statements:</p>
<blockquote class="twitter-tweet" data-lang="en">
<p dir="ltr" lang="en">library(rvest)<br>library(tidyverse)</p>
<p>h &lt;-read_html(&quot;<a rel="nofollow" href="https://t.co/gloY1eErBn" target="_blank">https://t.co/gloY1eErBn</a>&quot;)</p>
<p>reps &lt;-h %>%<br> html_node(&quot;<a rel="nofollow" href="https://twitter.com/hashtag/mw?src=hash&ref_src=twsrc%5Etfw" target="_blank">#mw</a>-content-text > div > table:nth-child(18)&quot;) %>%<br> html_table()</p>
<p>reps &lt;-reps[,c(1:2,4:9)] %>%<br> as_tibble() <a rel="nofollow" href="https://t.co/25ANm7BHkj" target="_blank">pic.twitter.com/25ANm7BHkj</a></p>
<p>— Julia Silge (@juliasilge) <a rel="nofollow" href="https://twitter.com/juliasilge/status/951639629182074880?ref_src=twsrc%5Etfw" target="_blank">January 12, 2018</a></p></blockquote>
<p></p>
<p>Since Twitter munges the URL in the third line when you cut-and-paste, here's a plain-text version of Julia's code:</p>
<div>
<pre>library(rvest)
library(tidyverse)

h <span>&lt;-</span> read_html(<span>&quot;https://en.wikipedia.org/wiki/Current_members_of_the_United_States_House_of_Representatives&quot;</span>)

reps <span>&lt;-</span> h <span>%>%</span>
 html_node(<span>&quot;#mw-content-text > div > table:nth-child(18)&quot;</span>) <span>%>%</span>
 html_table()

reps <span>&lt;-</span> reps[,c(<span>1</span><span>:</span><span>2</span>,<span>4</span><span>:</span><span>9</span>)] <span>%>%</span> as_tibble()
</pre>
</div>
<p>And sure enough, here's what the <code>reps</code> object looks like in the RStudio viewer:</p>
<p><a rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2d32cf5970c-pi" target="_blank"><img border="0" alt="Scrape" title="Scrape" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2d32cf5970c-800wi"></a></p>
<p>As Julia <a rel="nofollow" href="https://twitter.com/juliasilge/status/951639773445156865" target="_blank">notes</a> it's not perfect, but you're still 95% of the way there to gathering data from a page intended for human rather than computer consumption. Impressive!</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2018/01/scraping-with-5-lines-r.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Jog0KPDRnzA" height="1"></a></div>