Web Scraping Tutorial: Handling Searches with Multiple Dropdown Menus

Sunday, October 30, 2016 11:12 PM

“Could I use a web scraping tool to extract the values from multiple drop-down menus directly, or should I write the codes myself when dealing with such web scraping requests?”

People often have the queries above when using the web scraping tools to extract data from multiple drop-down menus, where the contents of another drop-down menu are dynamically linked to what you choose in the first one.

In this tutorial, I will use Census of India for an example to show you how to scrape web data from websites with drop-down list using a web scraping tool.

(Download the extraction task of this tutorial HERE )

 

Step 1.

Open our web scraping application Octoparse. ➜ Choose “Advanced Mode”. ➜ Click “Start” ➜ Complete basic information.

Enter the target URL in the built-in browser. ➜ Click “Go” icon to open the web page.

(URL of the example:

http://www.censusindia.gov.in/Census_Data_2001/Village_Directory/View_data/Village_Profile.aspx )

 

Step 2.

Now we come to extract the values from the drop-down lists. There are four drop-down menus on the website. In this tutorial I will take the values in the last two drop-down menus as the loop items in web scraping.

Assume that we want to find the profile of Andamans District in Andaman and Nicobar Islands State, here we will extract the values from the "Select state" and "Select districts" drop-down menus during the web scraping process.

Click "Select state" and choose "Loop switch combobox".

We need to change the XPath here as we just want the value Andaman and Nicobar Islands in the “Loop Item” box. Just change the position of the XPath and click “Save”.

 

The same way to extract the value Andamans under “Switch Dropdown” action. Remember you should wait until the web page fully loads.

 

Step 3.

Now we come to loop the values in the last two drop-down menus.

Click "Select Sub-district" under “Switch Dropdown” action and choose "Loop switch combobox".

We need to change the XPath here as we don't want the value "<-Select Sub-district->” in the Loop Item box. Just change the position of the XPath and click “Save”. Then you could get what you delete the value "<-Select Sub-district->” in web scraping.

 

The same way to extract the values in the “Select village” drop-down list under “Switch Dropdown” action.

 

Then we could search the result. Click "Submit"under the last "Switch Dropdown" action in the Workflow Designer. ➜Choose "Click an item".

 

Step 4.

We can extract the text from sub-district and village first as the layout of this list is not similar to the information below.

Click "Diglipur" and choose "Extract text". The same way to extract “Aerial Bay”. ➜Modify the name in the “Field Name”.

 

Now we can extract the sections with similar layout. Move your cursor over those target sections, where you would extract data in web scraping.

Click the first highlighted link ➜ Click “TR” to expand the selected area. ➜Click “Create a list of items” (sections with similar layout). ➜ Click “Add current item to the list”.

Then the first highlighted link has been added to the list. ➜ Click “Continue to edit the list”.

Click the second highlighted link. ➜ Click “TR” to expand the selected area. ➜ Click “Add current item to the list” again.

Now we get all the links with similar layout in web scraping. ➜Then click “Finish Creating List” ➜ Click “loop” to process the list for extracting the elements from each page.

 

You would find that some lists have two fields while some only have one, so we could extract the data from the second item "Area of village" as it has another field "81.86", which is missing in the first item.

Just manually choose the second item "Area of village (in hectares)" in the Workflow Designer and extract the data.

Extract the title of the "Area of village (in hectares)". ➜ Click and select “Extract text”.

 

Step 5.

All the contents will be selected in Data Fields. ➜ Click the “Field Name” to modify.

 

Step 6.

Click “Next” ➜ Click “Next” ➜ Click “Local Extraction” to run the task on your computer. Octoparse will automatically extract all the data selected.

 

Step 7.

The data extracted will be shown in “Data Extracted” pane. Click “Export” button to export the results to Excel file, databases or other formats and save the file to your computer.

 

Author: The Octoparse Team 

Download Octoparse Today

For more information about Octoparse, please click here.

 

Author's Picks

Scrape Web Data from A Drop-Down Menu 1

Scrape Web Data from A Drop-Down Menu 2

Pagination: Scrape Data from Websites with Query Strings (1)

Pagination: Scrape Data from Websites with Query Strings (2)

Getting started with XPath 1

Getting started with XPath 2

Getting started with XPath 1

Collect Data from LinkedIn

30 Free Web Scraping Software

Collect Data from Amazon

Top 30 Free Web Scraping Software

- See more at: http://www.octoparse.com/tutorial/pagination-scrape-data-from-websites-with-query-strings-2/#sthash.gDCJJmOQ.dpuf
Request Pro Trial Data
Collection
Service
Email
us

Leave us a message

Your name*

Your email*

Subject*

Description*

Attachment(s)

Attach file
Attach file
Please enter details of your issue and we will get back to you ASAP.
× get my coupon now No Thanks