All pages are not created equal. So when your target web pages are showing variations, you should use "Branch conditions" to achieve condition-based scraping. Here is how it works:
When should you consider using "Branch Conditions"?
There are two main scenarios when the Branch Conditions can be useful:
When you are only interested in getting data from certain pages with a specific tag, such as "New", "Hot selling", "On Sale" etc.
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 as a condition to test for: if the element is found on the item page, we’ll go ahead and capture the product information; otherwise, we’ll skip the page/product entirely.
Let’s see how it is done! To follow through, you may want to use this URL in the tutorial:
STEP 1. Build a loop to click each link on the list (see tutorial)
STEP 2. Use "Branch Conditions" to test for the condition: whether
is present on the item page
Go to the workflow section and click on the add step button to add a Branch Conditions inside the loop
Select "Branch Conditions_Branch2" on the left-hand side and select "Execute if the current page contains specific element"
Fill in the XPath for element
: "//div[@class='pricing-price__savings']" into the text box below (What is XPath?).
NOTE: If you don't know how to write an XPath, you can use the XPath generator button and select the element on the page, Octoparse would then generate an XPath automatically.
Click the branch on the right-hand side, select "Always execute the branch"
Difference between the 5 condition actions:
STEP 3. On the product item page (select one item from the loop that has the element
), click on any desired data fields to capture (learn how). Rename the fields if needed.
STEP 4. Drag the "Extract Data" action into the left branch
So now, we have configured Octoparse to look for the element on the page. If the element is found, capture the desired data, otherwise, skip the product.
1. If you want to add more conditions to classify more variations, you can add it in the worklfow section.
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.