3 Ways to Scrape Data from a Table
Tags: datascience, webscraping, startup
Published: 2020-06-30 10:25:07

There is a lot of data presented in a table format inside the web pages. However, it could be quite difficult when you try to store the data into local computers for later access. The problem would be that the data is embedded inside the HTML which is unavailable to download in a structured format like CSV. Web scraping is the easiest way to obtain the data into your local computer.

 

![scrape data from a table_Octoparse](https://www.octoparse.com/media/7458/unicorn-startup.jpg?width=647&height=341)

 
I would love to introduce 3 ways of scraping data from a table to those who barely know anything about coding:


* Google Sheets
* Octoparse (web scraping tool)
* R language (using rvest Package)
 

##Google Sheets##
In Google sheets, there is a great function, called [Import Html](https://support.google.com/docs/answer/3093339?hl=en) which is able to scrape data from a table within an HTML page using a  fix expression, =ImportHtml (URL, "table", num).


**Step 1:** Open a new Google Sheet, and enter the expression into a blank.

A brief introduction of the formula will show up.

 ![Googlr sheet_importHTML](https://www.octoparse.com/media/7461/google-sheets_-table-format.jpg)

**Step 2:** Enter the URL (example: https://en.wikipedia.org/wiki/Forbes%27_list_of_the_world%27s_highest-paid_athletes) and adjust the index field as needed.

![Google Sheet_Scrape table](https://www.octoparse.com/media/7460/google-sheets_-scrape-data.jpg)

With the above 2 steps, we can have the table scraped to Google sheet within minutes. Apparently, Google Sheets is a great way to help us scrape table to Google sheets directly. However, there is an obvious limitation. That would be a such mundane task if we plan scrape tables across multiple pages using Google Sheets. Consequently, you need a more efficient way to automate the process.

 

##Scrape tables with a web scraping tool##
To better illustrate my point, I will use this website to show you the scraping process, [https://www.babynameguide.com/categoryafrican.asp?strCat=African](https://www.babynameguide.com/categoryafrican.asp?strCat=African)

First of all, download Octoparse and launch it.

**Step 1:** Click Advanced Mode to start a new project.

 ![Octoparse-Advanced Mode](https://www.octoparse.com/media/7462/octoparse-advanced-mode.jpg)

**Step 2:** Enter the target URL into the box and click “Save URL” to open the website in Octoparse built-in browser.

 ![Enter the URL](https://www.octoparse.com/media/7463/octoparse-enter-the-url.jpg)

**Step 3:** Create a pagination with 3 clicks:

a) Click “B” in the browser

b) Click “Select all” in the “Action Tips” panel

c) Click “Loop click each URL”  in the “Action Tips” panel

 ![Octoparse_Pagination 1](https://www.octoparse.com/media/7464/octoparse_pagination-1.jpg)       
 ![Octoparse_Pagination 2](https://www.octoparse.com/media/7465/octoparse_pagination-2.jpg)

Now, we can see a pagination loop has been created in the workflow box.

 ![Octoparse_Pagination 3](https://www.octoparse.com/media/7466/octoparse_pagination-3.jpg)

**Step 4:** Scrape a table with the below clicks.

a) Click on the first cell in the first row of the table

b) Click on the expansion icon from “Action Tips” panel until the whole row is highlighted in green color (usually the tag should be TR)

c) Click on “Select all sub-elements” in the “Action Tips” panel, then "Extract data" and “Extract data in the loop”

![Octoaprse-scrape a table 1](https://www.octoparse.com/media/7467/octoaprse-scrape-a-table-1.jpg)
![Octoaprse-scrape a table 3](https://www.octoparse.com/media/7469/octoaprse-scrape-a-table-3.jpg)
![Octoaprse-scrape a table 2](https://www.octoparse.com/media/7468/octoaprse-scrape-a-table-2.jpg)
![Octoaprse-scrape a table 4](https://www.octoparse.com/media/7470/octoaprse-scrape-a-table-4.jpg)


The loop for scraping the table is built in the workflow.

![Octoaprse-scrape a table 5](https://www.octoparse.com/media/7471/octoaprse-scrape-a-table-5.jpg)

**Step 5:** Extract data

![extract data](https://www.octoparse.com/media/7472/extract-data.jpg)

With the above 5 steps, we’re able to get the following result.

As the pagination function is added, the whole scraping process becomes more complicated. Yet, we have to admit that Octoparse is better at dealing with scraping data in bulk.

And the most amazing part is, we don’t need to know anything about coding. That said, whether we are programmers or not, we can create our “crawler”  to get the needed data all by ourselves. To obtain further knowledge of scrape data from a table or a form, please refer to [Can I extract a table/form](https://helpcenter.octoparse.com/hc/en-us/articles/360018324051-Can-I-extract-a-table-form-)?

However, if you happen to know some knowledge about coding and want to write a script on your own, then using the rvest package of R language is the simplest way to help you scrape a table.

 

##R language (using rvest Package)##
In this case, I also use this website, [https://www.babynameguide.com/categoryafrican.asp?strCat=African](https://www.babynameguide.com/categoryafrican.asp?strCat=African) as an example to present how to scrape tables with rvest.

Before starting writing the codes, we need to know some basic grammars about rvest package.

 

html_nodes() : Select a particular part in a certain document. We can choose to use CSS selectors, like html_nodes(doc, "table td"), or xpath selectors, html_nodes(doc, xpath = "//table//td")

html_tag() : Extract the tag name. Some similar ones are html_text (), html_attr() and html_attrs()

html_table() : Parsing HTML tables and extracting them to R Framework.

 

Apart from the above, there are still some functions for simulating human’s browsing behaviors. For example, html_session(), jump_to(), follow_link(), back(), forward(), submit_form() and so on.

 

In this case, we need to use html_table() to achieve our goal, scraping data from a table.

Download R([https://cran.r-project.org/](https://cran.r-project.org/)) first.

 

**Step 1:** Install rvest.

![Install rvest](https://www.octoparse.com/media/7474/install-rvest.jpg)

**Step 2:** Start writing codes as the below picture shows.

Library(rvest):  Import the rvest package
Library(magrittr) : Import the magrittr package
URL: The target URL
Read HTML : Access the information from the target URL
List: Read the data from the table


Step 3: After having all the code written in the R penal, click “Enter” to run the script. Now we can have the table information right away.

![rvest_final result](https://www.octoparse.com/media/7475/rvest_final-result.jpg)

It seems that it doesn’t take less effort in using a web scraping tool than in writing a few lines of codes to extract table data. In fact, programming does have a steep learning curve which raises the threshold for people, in general, getting into the real power of web scraping. This situation makes people who don't work in the tech industry harder to gain a competitive edge to leverage web data.


I hope the above tutorial will help you have a general idea of how a web scraping tool can help you achieve the same result as a programmer does with ease.