undefined

Web Scraping Tutorial: Branch Judgement

Wednesday, March 8, 2017 2:45 AM

For the latest tutorials, visit our new self-service portal. Sharpen your skills and explore new ways to use Octoparse.

 

Not all pages are created equal. When web pages are showing variations, you can use "Branch conditions" to achieve condition-based scraping. Here is how it works:
what_is_branch_1

 

When should you consider using "Branch Conditions"?

 There are two main scenarios when the "Branch Conditions" can be useful.
1) When you are only interested in getting data from certain pages with a specific tag, such as "New", "Hot selling", "On Sale" etc.
2) When data on the page is being displayed in a different pattern, i.e. sometimes showing up as text, other times showing up as images.

 

In the example image below, we want information about laptops that are on sale. Looking closely at the item detail page, it seems like we can use the "on-sale" icon save_icon as a condition to test for: if the element save_icon is found on the item page, we’ll go ahead and capture the product information; otherwise, we’ll skip the page/product entirely. 

branch_list

 
Let’s see how it is done! To follow through, you may want to use this URL in the tutorial:
 
1. Build a loop to click each link on the list (see tutorial)
2. Use "Branch Conditions" to test for the condition: whether save_icon is present on the item page
    1)Hover your mouse over the down_icon where you want to add the branch condition
    2)Click on plus_icon to add a "Branch Conditions" action inside the loop 
    3)Click "Branch Conditions_Branch2"  on the left-hand side and select "Execute if the current page contains specific elements
    4)Fill in the XPath for element save_icon : "//div[@class='pricing-price__savings']" into the text box below (What is XPath?). 
    5)Click "Apply"
  

Tips! If you don't know how to write an XPath, you can click mceclip1.png and select the element on the page. Octoparse would generate an XPath automatically.

branch_left

 

    6)Click the branch on the right-hand side, select "Always execute the branch"

branch_right

 

Tips! In Octoparse, you can set the condition to one of the following:

1. Always execute the branch When this option is selected, Octoparse will not judge at all and will proceed to execute the actions within the branch immediately. Only select this option for the branch on the right side.

2. Execute if the page contains specific text When selected, Octoparse will look for the designated text string within the current page.

3. Execute if the current page contains a specific element When selected, Octoparse will look for the designated element (according to the XPath filled in) within the current page.

4. Execute if the current loop contains specific text When selected, Octoparse will look for the designated text string within the current loop item.

5. Execute if the current loop contains a specific element When selected, Octoparse will look for the designated element (according to the Relative XPath filled in) within the current loop item. Use this option only when you need to judge between items of a loop.

3. On the product item page (select one item from the loop that has the element save_icon ), click on any desired data fields to capture (learn how). Rename the fields if needed.

4. Drag the "Extract Data" action into the left branch

So now, we have configured Octoparse to look for the element  save_icon on the page. If the element is found, capture the desired data, otherwise, skip the product. 

Tips!

1. If you want to add more conditions to classify more variations, you can click plus_icon to add a "Branch Conditions" action inside the loop 

2. If a condition is set as "whether an element is found", the designated element must be uniquely found on the page or the judgment may fail to work. 

3. Octoparse goes through the branches from left to right by default. It is important to always keep the condition you want to test for within the left branch; if the condition for the left branch is "Always execute the branch", Octoparse will not proceed to the branch on the right as "Always execute the branch" will always be tested "True". 

4. You can leave the branch blank if no data extraction action is needed when the condition is not met.

5. When a data extraction action is being added to both branches, both the number of the data fields and the name of the data fields are required to be kept the same.

6. You can use nested branch judgment to further refine the test. 

 

Author: The Octoparse Team

 

Download Octoparse Today

 

For more information about Octoparse, please click here.

Sign up today.

We use cookies to enhance your browsing experience. Read about how we use cookies and how you can control them by clicking cookie settings. If you continue to use this site, you consent to our use of cookies.
Accept decline